jsonecho

Java Escape

Paste your text and get it escaped for Java, ready to paste into code or data.

Loading the tool...

How to use Java Escape

  1. Paste or type the text you want to escape.
  2. Adjust the options if you need to.
  3. Copy the escaped result.

About Java Escape

In Java every double quote and backslash inside a string literal needs a backslash in front of it, and a line break has to be written as \n. Pasting a JSON payload, a SQL statement or a regular expression into a test class means doing that for every quote and backslash by hand, and regular expressions double the backslashes again.

The tool produces exactly the escapes the Java language specification defines, so the output can be placed between double quotes in your source. If you are on Java 15 or later you can also use text blocks, which need far less escaping.

What gets escaped

Backslash becomes \\, a double quote becomes \", newline \n, carriage return \r, tab \t, backspace \b, form feed \f. Other control characters turn into \uXXXX.

Frequently asked questions

Do I need to escape single quotes in a Java string?

Not inside a double quoted string, only inside a char literal. There is an option if you need it.

Why are my regex backslashes doubled?

Java escapes the backslash in the string first, and the regex engine then reads the result. A pattern for a digit, \d, has to be written "\\d" in the source.

How does this differ from the JavaScript escape?

Java does not escape single quotes by default and has no \v or \x escapes, so the output differs for a few characters.

Is there an easier way for long text?

From Java 15 on, text blocks with triple quotes let you paste multi-line text without escaping line breaks and quotes.

More in Escape and Text

All tools →