In Java, translateEscapes() returns a new String in which every escape sequence that appears as plain text is replaced by the character it stands for. A backslash followed by n (two chars) becomes one newline char, \t becomes a tab, \" a quote, and so on.
The compiler does this for string literals in your source code; the method does the same for text that arrives at runtime, for example from a configuration file. It understands the same escapes as Java literals, including octal escapes like \101.
translateEscapes() is an instance method of java.lang.String, added in Java 15. Call it on a string, as in text.translateEscapes(); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 15 or newer.
Syntax
Call it with no arguments on text that contains backslash escapes.
java
String raw = config.get("greeting"); // e.g. Hello\nWorld as typed text
String real = raw.translateEscapes();
Call
What it does
"a\\tb".translateEscapes()
a, a real tab char, then b.
"\\101".translateEscapes()
"A" (octal 101 = 65).
"x\\sy".translateEscapes()
"x y": \s is a space.
Supported escapes are \b \t \n \f \r \s \" \' \\, octal escapes from \0 to \377, and a backslash before a line terminator (a line continuation, which removes both). Unicode escapes such as \u2022 are not translated.
Library declarations
java
public String translateEscapes();
Parameters
Parameter
Description
None
translateEscapes() takes no parameters.
Return Value
Return Value
Description
String
The text with every escape sequence replaced by its character.
Java Examples
Example 1 — Turn Typed Escapes into Real Characters
The raw text contains a backslash and an n, not a line break, until it is translated.
java
public class TranslateEscapesBasic {
public static void main(String[] args) {
String raw = "Line 1\\nLine 2 \\\"quoted\\\"";
String real = raw.translateEscapes();
System.out.println(raw);
System.out.println(real);
}
}
Output
Line 1\nLine 2 \"quoted\"
Line 1
Line 2 "quoted"
How It Works
Doubled backslashes in source.\\n in the literal stores a backslash and an n, so printing raw shows them as text.
Translate at runtime.translateEscapes() turns that pair into one newline char and each \" into a plain quote.
Two different strings.real prints on two lines with real quotes, while raw is unchanged.
Example 2 — Octal Escapes and the \s Space Escape
Letters given as octal codes plus the \s escape added in Java 15.
java
public class TranslateEscapesOctal {
public static void main(String[] args) {
String raw = "A\\101\\s\\102";
String real = raw.translateEscapes();
System.out.println("[" + real + "]");
System.out.println(raw.length() + " -> " + real.length());
}
}
Output
[AA B]
11 -> 4
How It Works
Octal codes.\101 is octal for 65 ('A') and \102 is 66 ('B').
A visible space escape.\s becomes a single space, the same escape text blocks use to keep trailing spaces.
Shorter result. The 11-char raw text collapses to the 4 chars A, A, space, B.
Example 3 — Line Continuations and Invalid Escapes
A backslash before a newline joins lines; an unknown escape is rejected.
java
public class TranslateEscapesErrors {
public static void main(String[] args) {
String joined = "one \\\ntwo".translateEscapes();
System.out.println(joined);
try {
"bad \\q escape".translateEscapes();
} catch (IllegalArgumentException e) {
System.out.println("\\q -> " + e.getClass().getSimpleName());
}
}
}
Output
one two
\q -> IllegalArgumentException
How It Works
Backslash plus newline. The literal "one \\\ntwo" holds a backslash followed by a real line break.
Continuation removes both.translateEscapes() deletes the pair, so the two words end up on one line.
Unknown escapes throw.\q is not a Java escape, so the method throws IllegalArgumentException instead of guessing.
Frequently Asked Questions
It replaces escape sequences written as text, such as \n, \t, or \", with the characters they represent, and returns a new String.
Java 15, together with text blocks and the \s escape.
No. Unicode escapes are processed by the compiler before parsing, not by this method. A \u in the text causes IllegalArgumentException.
IllegalArgumentException for an unknown escape sequence or a lone backslash at the end of the text.
\b, \t, \n, \f, \r, \s, \", \', \\, octal escapes up to \377, and a backslash before a line terminator.