如何在Java中打印转义字符

时间:2021-02-22 00:26:48

When I have a string such as:

当我有一个字符串,如:

String x = "hello\nworld";

How do I get Java to print the actual escape character (and not interpret it as an escape character) when using System.out?

在使用System.out时,如何让Java打印实际的转义字符(而不是将其解释为转义字符)?

For example, when calling

例如,在打电话时

System.out.print(x);

I would like to see:

我想看看:

hello\nworld

And not:

helloworld

EDIT:My apologies, I should clarify the question. I have no control over what 'x' is going to be, it may be a string I read from a file. I would like for x to retain its escape characters during normal program execution, but for debugging purposes I would like to see the actual escape characters.

编辑:道歉,我应该澄清这个问题。我无法控制'x'是什么,它可能是我从文件中读取的字符串。我希望x在正常程序执行期间保留其转义字符,但出于调试目的,我希望看到实际的转义字符。

Writing a method that subs each and every escape character seems overkill. Isn't there a better printing library other than PrintStream that does this for me?

编写一个包含每个转义字符的方法似乎有些过分。是否有比PrintStream更好的打印库为我这样做?

If you're familiar with Ruby, think x.inspect()

如果您熟悉Ruby,请考虑x.inspect()

8 个解决方案

#1


15  

One way to do this is:

一种方法是:

public static String unEscapeString(String s){    StringBuilder sb = new StringBuilder();    for (int i=0; i<s.length(); i++)        switch (s.charAt(i)){            case '\n': sb.append("\\n"); break;            case '\t': sb.append("\\t"); break;            // ... rest of escape characters            default: sb.append(s.charAt(i));        }    return sb.toString();}

and you run System.out.print(unEscapeString(x)).

并运行System.out.print(unEscapeString(x))。

#2


22  

Use the method "StringEscapeUtils.escapeJava" in Java lib "org.apache.commons.lang"

在Java库“org.apache.commons.lang”中使用方法“StringEscapeUtils.escapeJava”

String x = "hello\nworld";System.out.print(StringEscapeUtils.escapeJava(x));

#3


1  

You have to escape the slash itself:

你必须逃避斜线本身:

String x = "hello\\nworld";

#4


1  

Just escape the escape character.

逃脱逃脱角色。

String x = "hello\\nworld";

#5


1  

System.out.println("hello \\nworld");

#6


0  

Java has its escape-sequence just the same as that in C.use String x = "hello\\nworld";

Java的转义序列与C.use String x =“hello \\ nworld”中的转义序列相同;

#7


0  

Try to escape the backslash like \\n

尝试逃避反斜杠,如\\ n

#8


-1  

You might want to check out this method. Although this may do more than you intend. Alternatively, use String replace methods for new lines, carriage returns and tab characters. Do keep in mind that there are also such things as unicode and hex sequences.

您可能想要查看此方法。虽然这可能比你想要的更多。或者,对新行,回车符和制表符使用String替换方法。请记住,还有unicode和十六进制序列之类的东西。

#1


15  

One way to do this is:

一种方法是:

public static String unEscapeString(String s){    StringBuilder sb = new StringBuilder();    for (int i=0; i<s.length(); i++)        switch (s.charAt(i)){            case '\n': sb.append("\\n"); break;            case '\t': sb.append("\\t"); break;            // ... rest of escape characters            default: sb.append(s.charAt(i));        }    return sb.toString();}

and you run System.out.print(unEscapeString(x)).

并运行System.out.print(unEscapeString(x))。

#2


22  

Use the method "StringEscapeUtils.escapeJava" in Java lib "org.apache.commons.lang"

在Java库“org.apache.commons.lang”中使用方法“StringEscapeUtils.escapeJava”

String x = "hello\nworld";System.out.print(StringEscapeUtils.escapeJava(x));

#3


1  

You have to escape the slash itself:

你必须逃避斜线本身:

String x = "hello\\nworld";

#4


1  

Just escape the escape character.

逃脱逃脱角色。

String x = "hello\\nworld";

#5


1  

System.out.println("hello \\nworld");

#6


0  

Java has its escape-sequence just the same as that in C.use String x = "hello\\nworld";

Java的转义序列与C.use String x =“hello \\ nworld”中的转义序列相同;

#7


0  

Try to escape the backslash like \\n

尝试逃避反斜杠,如\\ n

#8


-1  

You might want to check out this method. Although this may do more than you intend. Alternatively, use String replace methods for new lines, carriage returns and tab characters. Do keep in mind that there are also such things as unicode and hex sequences.

您可能想要查看此方法。虽然这可能比你想要的更多。或者,对新行,回车符和制表符使用String替换方法。请记住,还有unicode和十六进制序列之类的东西。