FormatterHQ

Java String Escape

Convert text into a valid Java string literal. Quotes, backslashes and line breaks are escaped and every non-ASCII character becomes a portable \uXXXX sequence.

Loading tool…
Ready.

How to use the Java Escape

  1. Type or paste into the Raw text box, or click Sample. You can also drop a text file on either box, or load one into the Escaped Java box with its Open file button.
  2. The Escaped Java box updates live as you type. It works both ways — edit the Escaped Java side to convert back, or click ⇄ Swap to exchange the sides.
  3. Use Copy or Download on either box to take the result, or Clear to start again.

Java escape for string literals

The Java escape tool prepares text for a "…" string literal in Java source code. It produces the same style of output as Apache Commons Text’s StringEscapeUtils.escapeJava(): readable ASCII stays readable, and everything that could upset the compiler or a non-UTF-8 build is escaped.

Escaping rules

  • \ → \\ and " → \".
  • Line feed, carriage return, tab, backspace and form feed → \n, \r, \t, \b, \f.
  • Any other control character and every non-ASCII character → \uXXXX. Characters above U+FFFF, like emoji, become a UTF-16 surrogate pair.
  • The single quote is left alone: it needs escaping only in a char literal ('\''), not in a string.

Example

Input:

Path "C:\data"
naïve 🚀 it's

Output:

Path \"C:\\data\"\nna\u00EFve \uD83D\uDE80 it's

Why non-ASCII is escaped

Java source files are only compiled correctly if javac reads them with the right encoding. Before Java 18 the default depended on the platform, so a literal é could turn into é on a Windows build server. Writing \u00E9 is pure ASCII and compiles identically everywhere, which is why many style guides and .properties files use it.

Java strings versus .properties files

Escaped text often ends up in a .properties file rather than in source code, and the rules there are close but not identical:

  • Encoding. Properties.load(InputStream) still reads files as ISO-8859-1, so non-ASCII characters must be written as \uXXXX, exactly as this tool produces them. Resource bundles have been read as UTF-8 since Java 9, but the \u form works in both cases.
  • Quotes. A properties value is not wrapped in quotes, so the escaped \" is unnecessary there (it still loads as a plain quote).
  • Keys. In keys, =, : and spaces must be escaped with a backslash, and a line starting with # or ! is a comment. Those rules do not apply to Java string literals.

To inspect a finished properties file as structured data, try Properties to JSON.

A Java-only pitfall: \u in comments

The Java compiler translates \uXXXX sequences before it tokenises the file. That means \u000A is a real line break even inside a comment or string, and "\u0022" closes the string. This tool therefore always uses \n, \" and friends for special characters and never emits a Unicode escape that could change the structure of your code.

Text blocks

Since Java 15 you can also write multi-line strings as text blocks ("""), where line breaks and quotes need no escaping. The escaped single-line form from this tool remains useful for annotations, older Java versions, generated code and constants.

In code, Apache Commons Text provides StringEscapeUtils.escapeJava() (the older Commons Lang version is deprecated), and code generators such as JavaPoet escape literals automatically. Use this tool when you are writing constants, test data or annotation values by hand.

Decode escaped strings with Java Unescape, or format a whole class with the Java Formatter. For C# code, see .NET (C#) Escape. Everything runs in your browser.

Frequently asked questions

Does this match StringEscapeUtils.escapeJava?
Yes, for practical purposes. Both escape quotes, backslashes and the usual control characters and turn non-ASCII characters into \uXXXX. Minor differences may exist in how rarely used control characters are written.
How do I put a newline in a Java string?
Use \n inside the literal, or \r\n if you need a Windows line ending. For text with many lines, a Java 15+ text block (""") is often more readable.
Why is my emoji turned into two \u escapes?
Java strings are UTF-16. Characters above U+FFFF, including most emoji, are stored as a surrogate pair, so they need two \uXXXX escapes, for example \uD83D\uDE80.
Do I need to escape single quotes in a Java string?
No. \' is only required inside a char literal. It is allowed in strings but unnecessary, so the tool leaves apostrophes as they are.

Last updated October 3, 2026

Related tools