Java Escape / Unescape
Escape plain text for use inside a Java string literal, or unescape a literal copied from source code back into the text it holds.
Input
Output
What Is Java String Escaping?
In Java, string literals must have certain characters escaped so the compiler can parse them correctly. Backslashes, double quotes, single quotes, newlines, tabs, carriage returns, backspaces, and form feeds all need to be represented as escape sequences. For example, a literal newline inside a Java string must be written as \n, and a backslash must be written as \\. This follows the rules defined in the Java Language Specification: String Literals.
This tool works in two modes: Escape turns raw text into a safe Java string literal, and Unescape converts an escaped Java string back to human-readable text. Both directions are handled by script on this page, so the string you are working on is not uploaded. For the full list of Java escape sequences, see Oracle's Java Character tutorial.
How to Use This Tool
Choose Mode
Select Escape to convert raw text to a safe Java string, or Unescape to convert an escaped Java string back to plain text.
Paste Your Text
Put the text in the input pane on the left. Sample drops in a short snippet with double quotes, a Windows path and a tab, and Upload reads a text file from disk.
Get the Result
The output is redrawn about 300 ms after you stop typing, and flipping the mode reruns it on whatever is already in the left pane. Download writes escaped.txt in Escape mode and unescaped.txt in Unescape mode. If the string is headed into a class you are tidying anyway, the Java Formatter.
Java Escape Examples
Here is a real-world example. The raw text contains a newline, a tab, a backslash, and double quotes:
Raw input (with newline, tab, backslash, and quotes)
Escaped Java string output
When Java String Escaping Matters
Any time you embed text in a Java string literal you need to escape special characters. This comes up when building SQL queries, constructing JSON strings manually, writing log messages with file paths, or embedding HTML inside a Java string. Forgetting to escape a backslash in a Windows file path (C:\Users\name) is one of the most common Java beginner bugs. The java.lang.String class documentation covers how strings are stored in the JVM after parsing.
Java 15+ introduced text blocks ("""...""") which reduce the need for manual escaping in multiline strings. See the JEP 378 Text Blocks spec for details. For JSON-specific escaping, try the JSON Escape tool.
Frequently Asked Questions
What characters need to be escaped in a Java string?
The characters that must be escaped in Java string literals are: backslash (\\), double quote (\"), single quote (\'), newline (\n), carriage return (\r), tab (\t), backspace (\b), and form feed (\f). Non-ASCII characters can also be represented as Unicode escapes like \u00E9.
What is the difference between \n and an actual newline in Java?
In a Java string literal, writing an actual newline character (pressing Enter) causes a compile error. You must use \n instead, which the compiler translates to the Unicode character U+000A (line feed). Java 15+ text blocks are the exception: they do allow real newlines inside the """...""" delimiters.
How do I escape a backslash in Java?
A single backslash in Java source code must be written as two backslashes: \\. So a Windows path like C:\Users\name must be written as "C:\\Users\\name" in a Java string literal. This is the most common escaping mistake for developers coming from other languages.
What are Java Unicode escape sequences?
Java supports Unicode escapes in the form \uXXXX where XXXX is a four-digit hexadecimal number representing the Unicode code point. For example, \u00E9 is é and \u4E2D is 中. These can appear anywhere in Java source code, not just in string literals, which is why Escape never writes a control character as \u000a: javac would turn that into a real line break before it reads the string. It uses octal instead (\013 for a vertical tab), and Unescape reads both forms back. See the JLS Unicode Escapes section for the full rules.
Does this tool send my data to a server?
No. The backslash handling is plain JavaScript executing in this tab, and nothing you type is transmitted. That makes it reasonable to escape a connection string, an API key or a password before pasting it into a Java constant.
Related Tools
If a string needs more than a couple of escapes, a Java 15+ text block (""") is usually easier to read than anything this page can produce.