How to add “ ” quotes around printed String? [duplicate]

♀尐吖头ヾ 提交于 2019-12-18 03:32:45

问题


I want to print inverted quotes in java. But how to print it?

for(int i=0;i<hello.length;i++) {
    String s=hello[i].toLowerCase().trim();
    System.out.println(""+s+"");
}

expected OP: "hi".....


回答1:


Because double quotes delimit String values, naturally you must escape them to code a literal double quote, however you can do it without escaping like this:

System.out.println('"' + s + '"');

Here, the double quote characters (") have been coded as char values. I find this style easier and cleaner to read than the "clumsy" backslashing approach. However, this approach may only be used when a single character constant is being appended, because a 'char' is (of course) exactly one character.




回答2:


As quotes are used in the Java source code to represent a string, you need to escape them to create a string that contains a quote

 System.out.println("\""+s+"\"");



回答3:


You must escape the quotes: \"




回答4:


Assuming that by "Inverted" quotes you meant "Left" and "Right" specific quotation marks, you could do it like this:

System.out.println('\u201C'+s+'\u201D'); // Prints: “s”
System.out.println('"'+s+'"');           // Prints: "s"



回答5:


If you are really looking for inverted quotes, use this:

System.out.println('\u201C' + s + '\u201D');

It'll output “hi”, not "hi".

You need to have a font installed, though, that supports this, otherwise you might get a box or something instead. Most Windows fonts do.



来源:https://stackoverflow.com/questions/15305956/how-to-add-quotes-around-printed-string

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