Java Escape and Unescape
ED-33Paste text to escape it for a Java string literal, or paste a Java string to get the real characters. Backslash, quote, newline, tab and \uXXXX escapes are supported.
Runs in your browser. Nothing is uploaded.
Text or Java string
Result
How to use it
- Paste your text or your Java string into the input box.
- For escaping, choose whether to write non-ASCII characters as \uXXXX, escape single quotes or add surrounding quotes.
- Click Escape or Unescape.
- Copy the result into your source code or download it.
What Escape produces
The tool writes \\ for a backslash, \" for a double quote and the short forms \n, \r, \t, \b and \f. Other control characters become three digit octal escapes such as \001. It avoids \u000A on purpose: the Java compiler translates \uXXXX before it reads the string, so \u000A would break the literal across lines. With the first option on, every non-ASCII character becomes \uXXXX, and an emoji becomes two escapes because Java strings use UTF-16.
What Unescape reads
Unescape handles the escapes above plus \s, \', octal escapes such as \101 and \uXXXX, including forms with several u letters. One pair of surrounding double quotes is removed. A broken escape gives an error with its position. Text blocks written with three quotes, a backslash at the end of a line and joined strings are not handled, and the result is not compiled or checked as Java code.
Error messages and what to do
| Message | What it means | Fix |
|---|---|---|
Unknown escape | A backslash is followed by a character that Java does not define as an escape. | Use a valid escape such as \n, or write \\ for a literal backslash. |
needs four hex digits | A \u is not followed by four hex digits. | Write exactly four hex digits after \u. |
Questions people ask
How do I escape a string in Java?
Put a backslash before a double quote or backslash, and write line breaks as \n and tabs as \t. This tool does it for you: paste the text, click Escape and copy the result between double quotes in your code.
How do I write a unicode character in a Java string?
Use \uXXXX with four hex digits, such as \u00e9 for é. Characters above U+FFFF need two escapes, a surrogate pair. Tick the non-ASCII option in this tool to get these automatically.
Why does Escape write \001 instead of \u0001 for control characters?
Java translates \uXXXX escapes before parsing, so \u000A becomes a real line break and ends the literal. Octal escapes are processed later, as part of the string, so they are safe for control characters.
Does it escape single quotes?
Only if you turn the option on. Single quotes need escaping in a char literal such as '\'' but not inside a double-quoted string, so the default leaves them alone.
Can I unescape a text block?
Not directly. Text blocks use three double quotes and their own indentation rules, which this tool does not apply. Paste the content of a normal string literal, with or without the surrounding quotes.
About this tool
Escape turns a backslash, double quote, line break, tab, backspace and form feed into the escapes Java source accepts, and other control characters into three digit octal escapes. Turn on the first option to write every non-ASCII UTF-16 unit as \uXXXX, so an emoji becomes two escapes. Unescape reads those, plus \s, \', octal and \uXXXX, and removes one pair of surrounding double quotes. Text blocks, a backslash at the end of a line and string concatenation are not handled, and the tool does not compile or check your code.