Java - 打印显示换行符的字符串

标签 java string printing console

<分区>

我希望将字符串打印到控制台,重要的是要看到确切的字符串,这意味着它应该在日志中显示换行符 \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/

相关文章:

ios - 使用 Swift 在 iOS 中打印 View

iphone - iOS 为 uiwebview 内容启用 AirPrint

java - 表单未将数据发布到 Spring Controller

java - Pig latin 程序,switch/else 错误

Java Box (BoxLayout) 无法按预期工作

string - D: 如何删除字符串中的最后一个字符?

c# - 在 ToString() 之前检查 null

javascript - 除非我设置断点,否则 Firefox 不会打印?

java - 了解 Struts2 内部结构 : Result Configuration

java - 访问 wicket 组件层次结构之外的 Wicket 页面对象