如何添加""打印字符串周围的引号? [英] How to add " " quotes around printed String?

查看:23
本文介绍了如何添加""打印字符串周围的引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 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:嗨".....

expected OP: "hi".....

推荐答案

因为双引号 delimit 字符串值,自然你必须将它们转义为文字双引号,但是你可以做到没有像这样逃脱:

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.

这篇关于如何添加"&quot;打印字符串周围的引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆