Java Escape

Escape special characters in Java strings.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What Java Escape does

Java Escape writes text as the contents of a Java string literal. The double quote and backslash are escaped, control characters become \n, \t and the other short escapes or \uXXXX, and every character outside ASCII is written as \uXXXX.

Writing non-ASCII characters as \u escapes means the code compiles the same whatever encoding the source file is saved in. This matches the behaviour of escapeJava in Apache Commons Text. Characters above U+FFFF, such as emoji, become two \u escapes (a surrogate pair), which is how Java stores them.

Java Escape examples

The built-in sample

Input

say "hello" & <b>bye</b>

Output

say \"hello\" & <b>bye</b>

This is the input that the Load sample button fills in.

Accents and emoji

Input

Zoë says "hi" 😀

Output

Zo\u00EB says \"hi\" \uD83D\uDE00

ë becomes \u00EB and the emoji becomes the surrogate pair \uD83D\uDE00.

Control characters

Input

Line 1
Line 2	Tabbed␇

Output

Line 1\nLine 2\tTabbed\u0007

Java has no short escape for U+0007 (bell), so it becomes \u0007.

Each output was produced by running this tool on the input above it. Invisible control characters are shown as symbols, for example ␁ for U+0001 and ␀ for U+0000.

How it works

Each character is checked against the rules below; printable ASCII is copied unchanged.

CharacterWritten as
"\"
\\\
line break, carriage return, tab\n \r \t
backspace, form feed\b \f
other control characters\u0007
any character above U+007E\u00EB (one escape per UTF-16 unit)

Details and limits

  • The apostrophe is not escaped. Java only needs \' inside a char literal such as '\''.
  • Inside a text block (""" … """) the quotes would not need escaping, but the escaped form is still valid there.

Questions

Is my data uploaded when I use Java Escape?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does Java Escape do?

Java Escape writes text as the contents of a Java string literal. The double quote and backslash are escaped, control characters become \n, \t and the other short escapes or \uXXXX, and every character outside ASCII is written as \uXXXX.

Are there any limits I should know about?

The apostrophe is not escaped. Java only needs \' inside a char literal such as '\''. Inside a text block (""" … """) the quotes would not need escaping, but the escaped form is still valid there.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.