在Java中,如何打印字符串的文字内容?例如:
String s="Hello \"world\"";
System.out.println(s);
将打印:
Hello "world"
我怎样才能打印这个?
Hello \"world\"
最佳答案
正如 @Blender 在评论中指出的那样,使用 println
得到的实际上是字符串的文字内容。但是,如果您想在 Java 程序中获取必须插入引号中的文本,那么您可以使用 Apache StringEscapeUtils
的 escapeJava
方法> 类。请参阅http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String)
String s = "Hello \"world\"";
String escaped = StringEscapeUtils.escapeJava(s);
System.out.println(escaped);
关于java - 如何在Java中输出字符串的文字内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21657602/