Java String use quotes without escape character

妖精的绣舞 提交于 2019-12-01 02:28:48

问题


I know that it is possible to write double quotes in Java strings by putting \ symbol before them. But if double quotes are used a lot in a string, is there a way to mark the string once, so there won't be a need to write the \ symbol before each? (just like in C#, it is possible to put the @ symbol before the string) Example:

String quotes = @"""""""""""""""""""";

instead of

String quotes = "\"\"\"\"\"\"\"\"\"\"\"";

回答1:


You can't. But if you are too lazy to escape each of the double quotes, there are some trick that can do that. For example:

String quotes = "....................".replace(".","\"");
System.out.println(quotes);

output: """"""""""""""""""""




回答2:


No, there is no such way to write a String literal with unescaped quotes.

You can write the text externally and load it at runtime.




回答3:


There is no way to escape all subsequent double quotes in code (that I know of). But, I recommend against hard-coding String literals. Instead, I suggest you use a ResourceBundle (or even Properties). One benefit being you don't have to escape String(s) you read in that way.



来源:https://stackoverflow.com/questions/26416461/java-string-use-quotes-without-escape-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!