27

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?

For example, when calling

System.out.print(x);

I would like to see:

hello\nworld

And not:

hello
world

I would like to see the actual escape characters for debugging purposes.

Michael
  • 41,989
  • 11
  • 82
  • 128
jabalsad
  • 2,351
  • 6
  • 23
  • 28
  • \ is called backslash. Escape character means something else –  Feb 16 '20 at 22:52
  • @user2587965 — An [escape character](https://en.wikipedia.org/wiki/Escape_character) in computing is "a character that invokes an alternative interpretation on the following characters in a character sequence." In a Java string, the backslash is an escape character. – M. Justin Sep 14 '21 at 15:24

9 Answers9

37

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

String x = "hello\nworld";
System.out.print(StringEscapeUtils.escapeJava(x));
JudeFeng
  • 378
  • 1
  • 3
  • 6
  • 4
    StringEscapeUtils.escapeJava in "org.apache.commons.lang" is Deprecated as of version 3.6, use commons-text StringEscapeUtils instead – pringi Jul 30 '18 at 09:51
16

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)).

Vlad
  • 18,195
  • 4
  • 41
  • 71
  • 1
    This was what I was looking for, but I didn't know I'd have to write a function that does this. This means knowing about all possible escape characters. I figured there was a library that already does this. – jabalsad Oct 25 '11 at 10:54
  • 2
    There is, I think, but I haven't used it. There's another Stack Overflow [question](http://stackoverflow.com/questions/3537706/howto-unescape-a-java-string-literal-in-java) on this. – Vlad Oct 25 '11 at 10:57
  • Thanks, I was looking for a similar question but just couldn't get the keywords right :) – jabalsad Oct 25 '11 at 11:01
2

You have to escape the slash itself:

String x = "hello\\nworld";
Francisco Paulo
  • 6,284
  • 26
  • 25
2

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

pgras
  • 12,614
  • 4
  • 38
  • 46
1

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

starrify
  • 14,307
  • 5
  • 33
  • 50
1

Just escape the escape character.

String x = "hello\\nworld";
BastiS
  • 452
  • 3
  • 11
1

Try to escape the backslash like \\n

FailedDev
  • 26,680
  • 9
  • 53
  • 73
0

The following code produces octal escape sequences. Modify the replacement function to produce what you need.

Pattern
 .compile ("[^\\p{Graph}]")
 .matcher ("A\tB\nC")
 .replaceAll
 (
   (final MatchResult r)
   -> String .format ("\\\\%03o", + r .group () .charAt (0)))

Another possibility would be to use String .format ("^%c", r .group () .charAt (0) + '@')—but it will only handle control characters.

-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.

G_H
  • 11,739
  • 3
  • 38
  • 82
  • Regex quoting will not provide the correct answer in this case. First, this method is likely to just wrap the input with `\Q` and `\E`. Second, even if it did escape each character, the values don't match at all. For instance, the tab character `\t` is a valid regex character and would not need escaping, while `.` would need escaping. – jdmichal Jun 11 '15 at 21:40