Mastodon
99Tools.net

Java Escape / Unescape

Our Java Escape / Unescape tool helps you handle special characters in your Java strings effortlessly. Simply paste your string, and we’ll convert it to a valid Java literal or revert it back to its original form in seconds.

About This Tool

This Java Escape / Unescape tool is designed to save you time and prevent errors in your code. Instead of manually finding and replacing special characters—a tedious and error-prone task—you can simply paste your string into the text box. With a single click on “Escape” or “Unescape,” the tool instantly provides the correct output, which you can then copy to your clipboard or download. It’s a straightforward, no-fuss utility for any Java developer.

How Java Escaping and Unescaping Works

In Java, some characters have special meanings. To use them literally inside a string, you need to “escape” them by putting a backslash (\) before them. Unescaping is the reverse process, turning the escaped sequence back into the actual character.

Java Escape

The following characters are reserved and must be escaped to be used inside a Java string. Our tool makes this conversion for you automatically!

  • Backspace is converted to \b
  • Form feed is converted to \f
  • Newline is converted to \n
  • Carriage return is converted to \r
  • Tab is converted to \t
  • Double quote (") is converted to \"
  • Backslash (\) is converted to \\

Java Unescape

If you have an escaped Java string, the unescape function will convert the following character sequences back to their original form.

  • \b is converted back to a backspace.
  • \f is converted back to a form feed.
  • \n is converted back to a newline.
  • \r is converted back to a carriage return.
  • \t is converted back to a tab.
  • \" is converted back to a double quote (").
  • \\ is converted back to a backslash (\).

Java Escape / Unescape Examples

Example 1 : Java Escape

Java

class JavaEscapeExample {
    public static void main(String[] args) {
        // This is the original, unescaped string using a text block for readability.
        String originalCode = """
            class HelloWorld {
            	public static void main(String[] args) {
            		// Prints "Hello, World" to the terminal window.
            		System.out.println("Hello, World");
            	}
            }
            """;

        // Manually creating the escaped version for demonstration.
        // In a real application, you might use a library for this.
        String escapedCode = "class HelloWorld {\\n\\tpublic static void main(String[] args) {\\n\\t\\t// Prints \\\"Hello, World\\\" to the terminal window.\\n\\t\\tSystem.out.println(\\\"Hello, World\\\");\\n\\t}\\n}";


        System.out.println("--- Original Code Snippet ---");
        System.out.println(originalCode);

        System.out.println("\n--- Escaped String Result ---");
        System.out.println(escapedCode);
    }
}

Escaped Java

class JavaEscapeExample {\n    public static void main(String[] args) {\n        // This is the original, unescaped string using a text block for readability.\n        String originalCode = \"\"\"\n            class HelloWorld {\n            \tpublic static void main(String[] args) {\n            \t\t// Prints \"Hello, World\" to the terminal window.\n            \t\tSystem.out.println(\"Hello, World\");\n            \t}\n            }\n            \"\"\";\n\n        // Manually creating the escaped version for demonstration.\n        // In a real application, you might use a library for this.\n        String escapedCode = \"class HelloWorld {\\\\n\\\\tpublic static void main(String[] args) {\\\\n\\\\t\\\\t// Prints \\\\\\\"Hello, World\\\\\\\" to the terminal window.\\\\n\\\\t\\\\tSystem.out.println(\\\\\\\"Hello, World\\\\\\\");\\\\n\\\\t}\\\\n}\";\n\n\n        System.out.println(\"--- Original Code Snippet ---\");\n        System.out.println(originalCode);\n\n        System.out.println(\"\\n--- Escaped String Result ---\");\n        System.out.println(escapedCode);\n    }\n}

Example 2: Java Unescape

Escaped Java

import java.util.ArrayList;

class ListExample {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        System.out.println("--- My Fruit List ---");
        for (String fruit : fruits) {
            // Greet each fruit!
            System.out.println("Hello, " + fruit + "!");
        }
    }
}

Unescaped Java

import java.util.ArrayList;\n\nclass ListExample {\n    public static void main(String[] args) {\n        ArrayList<String> fruits = new ArrayList<>();\n        fruits.add(\"Apple\");\n        fruits.add(\"Banana\");\n        fruits.add(\"Cherry\");\n\n        System.out.println(\"--- My Fruit List ---\");\n        for (String fruit : fruits) {\n            // Greet each fruit!\n            System.out.println(\"Hello, \" + fruit + \"!\");\n        }\n    }\n}
RECOMMENDED
HTML Escape / Unescape
Try Now âž”