简体   繁体   English

转义字符'\\'不在System.out.println()中显示,但在返回值中显示

[英]Escape character '\' doesn't show in System.out.println() but in return value

In Java, when I replace characters in a String with escaped-characters, the characters show up in the return value, although they were not there according to System.out.println . 在Java中,当我将String的字符替换为转义字符时,这些字符会显示在返回值中,尽管根据System.out.println ,这些字符并不存在。

String[][][] proCategorization(String[] pros, String[][] preferences) {
String str = "wehnquflkwe,wefwefw,wefwefw,wefwef"; 
String strReplaced = str.replace(",","\",\""); //replace , with ","
System.out.println(strReplaced);

The console output is: wehnquflkwe","wefwefw","wefwefw","wefwef 控制台输出为: wehnquflkwe","wefwefw","wefwefw","wefwef

String[][][] array3d = new String[1][1][1]; // initialize 3d array
array3d[0][0][0] = strReplaced;
System.out.println(array3d[0][0][0]);


    return array3d;
}

The console output is: 控制台输出为:

wehnquflkwe","wefwefw","wefwefw","wefwef wehnquflkwe”,“ wefwefw”,“ wefwefw”,“ wefwef”

Now the return value is: 现在返回值为:

[[["wehnquflkwe\\",\\"wefwefw\\",\\"wefwefw\\",\\"wefwef"]]] [[[“ wehnquflkwe \\”,\\“ wefwefw \\”,\\“ wefwefw \\”,\\“ wefwef”]]]

I don't understand why the \\ show up in the return value but not in the System.out.println . 我不明白为什么\\显示在返回值中,而不显示在System.out.println

Characters in memory can be represented in different ways. 内存中的字符可以用不同的方式表示。

Your integrated development environment (IDE) has a debugger that chooses to represent a String[][][] with a single element that contains the characters 您的集成开发环境(IDE)有一个调试器,该调试器选择用一个包含字符的单个元素表示一个String[][][]

wehnquflkwe","wefwefw","wefwefw","wefwef

as a java-quoted string 作为用Java引用的字符串

"wehnquflkwe\",\"wefwefw\",\"wefwefw\",\"wefwef" 

this makes a lot of sense, because you can then copy and paste this string into java code without any loss. 这很有意义,因为您可以随后将该字符串复制并粘贴到Java代码中而不会造成任何损失。

On the other hand, your system's console, and the IDE's built-in terminal emulator, will output the characters in their normal representation, that is, without any java string-escape-characters: 另一方面,系统的控制台和IDE的内置终端仿真器将以正常表示形式输出字符,即没有任何java string-escape-characters:

wehnquflkwe","wefwefw","wefwefw","wefwef

As an experiment, you may want to check what happens with other "special" characters, such as \\t (a tab break) or \\b (backspace). 作为实验,您可能需要检查使用其他“特殊”字符,例如\\t (制表符)或\\b (退格键)会发生什么。 This is just the tip of the iceberg - characters in Java generally translate into unicode points, which may or may not be supported by the fonts available in your system or terminal. 这只是冰山一角 -Java中的字符通常会转换为unicode点,系统或终端中可用的字体可能支持也可能不支持。 The IDE's way of representing characters as java-quoted strings allows it to losslessly represent pretty much anything; IDE将字符表示为用Java引用的字符串的方式使它可以无损地表示几乎所有内容。 System.out.println's output is a lot more variable. System.out.println的输出具有更多的可变性。

System.out.println prints the String exactly as it is stored in memory. System.out.println与存储在内存中的String完全相同。

On the other hand, when you stop the application flow using a breakpoint you are able to look up the values. 另一方面,当您使用breakpoint停止应用程序流时,您可以查找值。

Most of the IDEs display escape characters with \\ to indicate that it's just one String , not String[] in this case, or not to split the String into two lines if it contains \\n in the middle. 大多数IDE用\\表示转义字符,以表示它只是一个String ,在这种情况下不是String[] ;如果中间包含\\n ,则不将String分成两行。

Just in case, you still have doubts, I suggest printing strReplaced.length() . 以防万一,您仍然有疑问,我建议打印strReplaced.length() This should allow you to count characters one by one. 这应该使您可以一一计算字符。

Possible experiments: 可能的实验:

String s = "my cute \n two line String";
System.out.println(s + " length is: " + s.length());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM