如何在Java输出中显示特殊的转义字符如LineBreak? [英] How to show special escape characters like LineBreak in Java output?

查看:816
本文介绍了如何在Java输出中显示特殊的转义字符如LineBreak?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找不到解决方案似乎是一个简单的问题。例如,我有一个字符串与LineBreaka \\\
a。此字符串的System.out.println输出显示2行。但是,我需要以完全相同的形式显示输出,即a \\\
a。如何实现这一目标?

Cannot find a solution to seem to be a simple question. For example, I have a string with LineBreak "a\na". The System.out.println output of this string shows 2 lines. However, I need to show the output in exactly the same form, i.e. "a\na". How to achieve this?

推荐答案

您需要转义 \\\
添加一个反斜杠。

You need to escape your \n with an extra backslash..

使用: - a\\\\
a
c $ c> \ n 。

Use: - "a\\na" to get \n in your output..

System.out.println("a\\na"); // Prints a\na

或者,您也可以查看: - Apache Commons StringEscapeUtils#escapeJava 将自动排除所有具有额外反斜杠的所有特殊转义序列

Or, you can also take a look at :- Apache Commons StringEscapeUtils#escapeJava which will automatically excape all the special escape sequences with an extra backslash..

String str = "a\nb\r\t";
String result = StringEscapeUtils.escapeJava(str); // Takes `\n` as `\\n`
System.out.println(result);

这将打印: - a\\\
b\r\t

这篇关于如何在Java输出中显示特殊的转义字符如LineBreak?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆