假设我有
String str="hello\" world\\";
打印str
时,输出为
hello" world\
即使在打印 str.length()
时,输出也是
13
有没有办法证明 str
值有转义字符?
最佳答案
运行时不存在转义字符。
转义字符只出现在字符串文字中。例如,
String literal = "Some\nEscape\rSequence\\\"";
在编译时,编译器生成一个 String
值及其实际的二进制表示形式 (UTF-8
iirc)。 JVM 直接使用该 String
值。
你写的
I am thinking that whenever we print a string and the output contains character such as " and
\
, then we can conclude that those character, " and\
was escaped?
这不是真的,这些字符可能是从文件或其他一些 InputStream
中读取的。它们绝对没有在文本文件中转义。
关于java - 确定字符串中是否存在转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22087227/