如何添加“ "打印字符串周围的引号? [英] How to add " " quotes around printed String?
问题描述
我想在java中打印反引号。但是如何打印?
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+"");
}
预期OP:hi.....
expected OP: "hi".....
推荐答案
因为双引号分隔字符串值,当然你必须将它们转义为代码文字双引号,但是你可以做它没有这样逃避:
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 + '"');
这里,双引号字符( )已被编码为
char
值。我发现这种风格比笨拙的反斜杠方法更容易阅读更清晰。但是,这种方法只能在正在追加单个字符常量,因为'char'(当然)只是一个字符。
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.
这篇关于如何添加“ "打印字符串周围的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!