creatorvalet Search

Java escape and unescape

Spell text so javac reads it back exactly, as a string literal or a text block, or turn escape sequences back into text.

  • SECURE
  • NO UPLOADS
  • NO SIGNUP
  • BROWSER BASED
  • FREE
  • FOREVER.

Support us with a link or a share

The result goes between the double quotes of an ordinary "…" literal.

Waiting
Characters in
Sequences written
Characters out
No. 2022waiting

Nothing at the counter yet.

A textarea rewrites CRLF to LF. Browsers do it to every form field before a script can read it, so pasted text arrives with 0 CRLF pairs and 0 lone LF whatever you copied. If your line endings matter, open the file instead.

Kind of literal

Javac reads café and 😀 as they are when the source file is compiled as UTF-8, the default since Java 18. Turn this on for older builds that use a platform encoding such as windows-1252.

What gets escaped, and why

CharacterWritten asWhy
\\\A backslash starts every escape, so a literal one is doubled first.
"\"Ends the string literal.
LF\n (raw in a text block)Line feed. A raw one is "unclosed string literal" — and so is \u000a, which javac turns into a real line break before it reads the string.
CR\rCarriage return. Same error raw or as \u000d, and a text block silently turns a raw one into LF.
Tab\tTab. Legal raw, but invisible, and trailing tabs vanish from a text block.
Backspace\bBackspace, U+0008.
Form feed\fForm feed, U+000C.
NUL, other controls\u0000 · \000 in a text blockNever \0 in a string literal: followed by a digit it becomes a longer octal escape and a different character. In a text block a unicode escape turns back into a raw character before trailing white space is stripped, so the octal form is used there.
three " in a row""\"Text block only. Three unescaped quote marks close the block. One or two are fine, so only every third is escaped.
space before a line break\sText block only. Javac strips white space at the end of every line before it reads escapes, so only an escaped space survives. White space with no escape of its own, such as U+3000, keeps its line by writing the break as \n\ and a line break.
lone surrogates\uXXXXAlways. A Java String can hold half of a surrogate pair, but UTF-8 cannot store one, so only its escaped form survives a copy or a download.
anything else\uXXXXOnly when you ask for ASCII-only output. Emoji become a surrogate pair such as \uD83D\uDE00. Java has no \x and no braces around a code point.

Unescaping reads every escape Java defines — the single-letter ones including \s, octal \0 to \377, and unicode escapes with one or more u — and never compiles or runs your text. Sequences javac rejects, such as \x41 or \q, are kept as written and listed with their position.

Nothing you type or open here is uploaded, put in the URL, or written to a log. The escaping runs in this tab.

What a Java escape does, character by character

Javac reads a string literal from its opening double quote to the next one that is not preceded by a backslash, and it gives up with “unclosed string literal” the moment it meets a line break. Any text that holds quotes, backslashes or several lines therefore has to be rewritten before it can live in a .java file. A Java escape replaces each of those characters with a short backslash sequence the compiler turns back into the original: \", \\, \n, \r and \t. Paste the result between quotes, compile, and the String in memory holds exactly the characters you started with.

A single quote needs nothing inside a string literal, so it is left alone, and so are accented letters and emoji. Control characters without a letter of their own, such as NUL, come out as \u0000, which means the same thing whatever follows it. Octal \0 would not: "\01" is a single U+0001, not a NUL and a digit.

The unicode escape trap that javac sets before it reads a string

Java handles \uXXXX differently from every other escape. The compiler substitutes those sequences across the whole source file before it looks for strings, comments or keywords. So \u000a is not an escaped newline at all: it turns into a real line feed in the middle of the literal and the build fails. The same happens to \u000d, while \u0022 closes the string early and \u005c becomes a backslash that pairs with the next character. This page never writes those four characters in that form, and when you unescape it flags them as errors rather than decoding them into something the compiler would never accept.

The reverse problem matters too. If your text contains the six characters \u0041 as documentation or test data, the backslash is doubled so the compiler sees an ordinary backslash followed by letters. Several u characters in a row, as in \uuuu0041, are legal Java and are read back correctly.

String literal or text block

Text blocks arrived in Java 15 and look like the easy answer for multi-line text, but the compiler edits their contents in three passes. It converts every line ending to a line feed, removes the indentation that all lines share plus any spaces at the end of each line, and only then interprets escapes. Paste a paragraph with a trailing space or a Windows carriage return and the String you get back is quietly different.

Choosing Text block produces the complete literal. Line breaks stay real, a space at the end of a line becomes \s, carriage returns become \r, control characters use three-digit octal escapes that survive the stripping pass, and every third quote mark in a row is escaped so the block cannot close early. The closing delimiter sits in the first column, which tells javac there is no incidental indentation to remove. Indent the whole block together in your editor and the value stays the same.

Reading escaped Java back into text

Unescape follows the Java Language Specification: the short escapes including \s, octal values from \0 up to \377, and unicode escapes, in the order the compiler applies them. In text block mode it also strips indentation and trailing space exactly as javac does and honors a backslash at the end of a line, which joins two lines. What Java rejects is kept as written and listed with its position: \x41, \u{41}, \v, \q, a raw double quote or line break inside a string literal. Unlike JavaScript, Java never quietly drops the backslash of an unknown escape, so neither does this reader.

Carriage returns survive a file, not a paste

Whatever you paste into the box above has already passed through the browser’s form handling, which rewrites each CR LF pair as a single LF. That is standard browser behavior and no page script can undo it; the counter under the field shows the endings that arrived. To keep Windows line endings, open the file with the button instead. The bytes are decoded directly, each carriage return is written as \r, and the compiled String contains CR LF again.

Why C# and .NET strings are not handled here

Escaping tools often lump Java and .NET together, and that pairing hides real differences. A C# \x escape swallows up to four hexadecimal digits, so "\x41BC" is one character rather than an A followed by BC. C# also has \U with eight digits, \a and \v, but no octal escapes and no compile-time unicode substitution. Output escaped for one language can compile in the other and mean something else, so only Java is supported, and every Java behavior described on this page was checked by compiling test strings with javac 21.

Related escapes and formatters

When the string travels in an API payload rather than source code, escape it as JSON, which has no octal and no text blocks. For a Spring or Maven configuration file, XML escaping uses entities such as &, and a template that renders into a web page needs HTML escaping. To inspect a JSON or XML value before embedding it, run it through the JSON formatter or the XML formatter, and test a pattern for Pattern.compile in the regex tester before doubling its backslashes here.

Private by construction

The work is string processing in this browser tab. Nothing you enter is sent anywhere, logged, or written into the page address, and nothing is compiled or run. Connection strings, tokens and proprietary source are typical things to escape, so feel free to watch the network tab in your developer tools while you use it.

Have an idea for this tool?

Tell us what would make this tool more useful, or suggest another tool you would like us to build.

Questions

Which characters have to be escaped in a Java string literal?

Only three are strictly required: the backslash, the double quote, and line breaks, since a raw line feed or carriage return between the quotes stops javac with “unclosed string literal”. This page also writes tabs, backspace and form feed as their short escapes, every other control character as a unicode escape, and unpaired surrogates too. Single quotes, accented letters and emoji are legal as they are, so they stay readable unless you ask for pure ASCII.

Why can I not write a line break as \u000a inside a Java string?

Because javac replaces unicode escapes with the characters they name before it splits the file into tokens. \u000a becomes a real line feed in the middle of the literal, which ends it with a compile error, and \u0022 becomes a quote that closes the string early. The escapes that work are \n, \r, \" and \\. This tool never writes those four characters as unicode escapes, and when you unescape it reports them as errors instead of quietly decoding them.

What is different when the result goes into a text block?

A text block, available since Java 15, keeps real line breaks, so they stay as they are. Javac also removes the indentation shared by every line and the white space at the end of each line, and turns every carriage return into a line feed. The tool therefore writes a trailing space as \s, carriage returns as \r, control characters as octal escapes, and breaks up three quote marks in a row. It returns the whole block with the closing delimiter in the first column, so no indentation is lost.

Does this also escape strings for C# and .NET?

No. C# looks similar but reads differently: its \x escape takes one to four hexadecimal digits, so the letters after it can silently become part of the code point, it has \U with eight digits and \a and \v, and it has no octal escapes and no early unicode translation. A string escaped for one language can compile to different text in the other. This page follows the Java Language Specification and nothing else.

Does my Java code leave the browser?

No. The escaping and the reading back are plain string work done by this tab, so there is nothing to send and no server that could keep a copy. Your text is not added to the address bar either. Long inputs move to a worker thread that this page starts from its own files, which keeps typing smooth. Nothing on the page compiles or executes what you paste, so keys, credentials and unreleased code are safe to work with here.