<分区>
我希望将字符串打印到控制台,重要的是要看到确切的字符串,这意味着它应该在日志中显示换行符 \n
。所以它将是 "this is the exact sequence of chars\n"
<分区>
我希望将字符串打印到控制台,重要的是要看到确切的字符串,这意味着它应该在日志中显示换行符 \n
。所以它将是 "this is the exact sequence of chars\n"
最佳答案
您可以将所有出现的 \n
字符替换为 \\n
(打印时将显示为 \n
)。您还应该将出现的 \r
字符替换为 \\r
,以防它存在。
String str = "this is the exact sequence of chars \n";
str = str.replace("\n", "\\n");
str = str.replace("\r", "\\r");
System.out.println(str);
关于Java - 打印显示换行符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001721/