Java String translateEscapes() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Java 15+

Definition

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();
CallWhat 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

ParameterDescription
NonetranslateEscapes() takes no parameters.

Return Value

Return ValueDescription
StringThe 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);
    }
}

How It Works

  1. Doubled backslashes in source. \\n in the literal stores a backslash and an n, so printing raw shows them as text.
  2. Translate at runtime. translateEscapes() turns that pair into one newline char and each \" into a plain quote.
  3. 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());
    }
}

How It Works

  1. Octal codes. \101 is octal for 65 ('A') and \102 is 66 ('B').
  2. A visible space escape. \s becomes a single space, the same escape text blocks use to keep trailing spaces.
  3. 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());
        }
    }
}

How It Works

  1. Backslash plus newline. The literal "one \\\ntwo" holds a backslash followed by a real line break.
  2. Continuation removes both. translateEscapes() deletes the pair, so the two words end up on one line.
  3. 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.

Next: trim()

Remove spaces and control characters (up to U+0020) from both ends.

trim() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful