How to convert 'unescaped string' to string in Java

Question above.

And by 'unescaped' I mean "Hello \\n \\\"World\\\"".

String myString = "Hello \\n \\\"World\\\"";
System.out.println("Before : \n");
String myConverted = escape(myString);
System.out.println("After : \n");


Before :

Hello \n \"World\"
After :


So I would be converting it to "Hello \n \"World\"".

I want it to escape the characters.

Is there a function to do this? escape is only an example.


  • You are using too much \s. To make new line you need \n, to make " will do \". Place 3 of \\\" and you get one \ escaped before escaped " Therefre if you generate this data it need some adjust. If you recieving it this way, unfortunately you have to make some rules of processing. Hovever since 15 java there is easy way:

        String myString = "Hello \\n \\\"World\\\"";
        System.out.println("Before : \n");
        String s = myString.translateEscapes();
        System.out.println("s = " + s);