Java String Escape and Unescape
ED-26Paste text to escape it for a Java string literal, or paste a literal to turn escapes like \n and \u00e9 back into characters. Non-ASCII escaping is optional.
Runs in your browser. Nothing is uploaded.
Text or Java string
Result
How to use it
- Paste the text, or the contents of a Java string, into the input box.
- Set the options: escape non-ASCII as \uXXXX, escape single quotes, add surrounding double quotes.
- Click Escape or Unescape.
- Copy the result, or download it as a .txt file.
What Escape writes
The backslash and double quote become \\ and \", and line breaks, tabs, backspace and form feed become \n, \t, \b and \f with \r for carriage return. Other control characters are written as three digit octal escapes such as \001. A \uXXXX escape would not work for them, because Java replaces unicode escapes before reading the string, so \u000A would break the literal across two lines.
Non-ASCII and single quotes
Characters outside ASCII are kept as they are unless you tick Escape non-ASCII, which writes every UTF-16 unit as \uXXXX. An emoji is two units, so it becomes two escapes. Single quotes need no escape inside a string, so they are left alone unless you tick that option, which is useful when the text goes into a char literal. The tool can also add the surrounding double quotes.
What Unescape reads
It reads \b \t \n \f \r \s \" \' and \\, octal escapes from \0 to \377, and \uXXXX, including several u letters. One pair of surrounding double quotes is removed if present. A backslash before a line break belongs to text blocks, and text blocks with triple quotes are not covered here. An unknown escape gives an error with the line and column.
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. | Double the backslash if you meant a literal one, or use a valid escape. |
needs four hex digits | A \u escape has fewer than four hex digits after it. | Write all four digits, for example \u00e9. |
would end the string | A double quote inside a quoted string is not escaped. | Write it as \". |
The escapes produce an unpaired surrogate | A \uD800 to \uDFFF escape does not form a valid character with its neighbour. | Pair a high surrogate (D800 to DBFF) with a low one (DC00 to DFFF). |
Questions people ask
How do I escape a string in Java?
Replace the backslash with \\, the double quote with \" and line breaks with \n, then put the result between double quotes. Paste the text here and click Escape to have that done, with the surrounding quotes added if you tick that box.
How do I turn \u00e9 back into é?
Paste the string and click Unescape. A \uXXXX sequence with four hex digits becomes the matching UTF-16 character, and two escapes that form a surrogate pair, such as \ud83d\ude00, become one character like the grinning face emoji.
Does it handle Java text blocks?
No. Text blocks with triple quotes use their own rules for line breaks and indentation, and a backslash before a line break is rejected here. The tool works on regular string literals, which is what most code uses.
Why are control characters written in octal and not as \u0001?
Java translates \uXXXX escapes early, before it reads the string. That is harmless for \u0001, but \u000A becomes a real line break and \u0022 a real quote, which break the literal. Octal escapes avoid that, so the tool writes them for every control character it has no short escape for.
Do I have to escape single quotes in a Java string?
No. Single quotes are allowed unescaped inside a string literal. They need escaping in a char literal, so there is an option for that. When unescaping, \' is accepted either way.
Is my text uploaded?
No. The conversion runs in your browser, so source code or strings you paste are not sent to a server. The tool needs no sign-up, and nothing is stored once you close or reload the page.
About this tool
Escape writes \\, \", \n, \r, \t, \b and \f. Other control characters become three digit octal escapes such as \001, because a \u000A in source is turned into a real line break before the string is read and would break the literal. With the option on, every non-ASCII UTF-16 unit becomes \uXXXX, so an emoji becomes two escapes. Unescape reads those plus \s, \', octal escapes and \uXXXX (including several u letters), and removes one pair of surrounding double quotes if present. Text blocks (""") and a backslash before a line break are not covered, and string concatenation or String.format placeholders are not touched.