dawsonc623
dawsonc623

Reputation: 2285

Java escaping escape sequence

I want to escape escape sequences in a string.

Example: if I had a string whose contents were "\n\u0073", I need to escape them in such a way that if I printed it to the command line, I would see

this:
\n\u0073
instead of:

s

I'll also be escaping double quotes (") and backslashes (\), and I figured out an expression to escape those already:

Pattern p = Pattern.compile("([\"\\\\])");
String str = p.matcher("\"\n\u0073\\"").replaceAll("\\\\$1");

This yields me:

\"
s\\

It doesn't take care of the escape sequences, though. What I want is:

\"\n\u0073\\

What modifications do I need to make to escape the escape sequences?

Upvotes: 5

Views: 5323

Answers (3)

The111
The111

Reputation: 5867

How about something like this? It works 100%... the only weak point is that I have an explicit case for each character needed. I'm not sure if there's a way around that, although perhaps you could get around that by making a case for an entire range of characters. I don't think RegEx can match a character definition like \u0073, but I don't know for sure.

public static void main(String[] args) {
    String unescaped = "\n\u0073";
    System.out.println("Version 1:\n" + unescaped);
    System.out.println("\nVersion 2:");
    printEscaped(unescaped);
}

public static void printEscaped(String unescaped) {
    for (char c : unescaped.toCharArray()) {
        switch (c) {
            case ('\n'):
                System.out.print("\\n");
                break;
            case ('\u0073'):
                System.out.print("\\u0073");
                break;
            default:
                System.out.print(c);
        }
    }
}

Output:

Version 1:

s

Version 2:
\n\u0073

Another potential problem for wider use is that it works on characters even if they weren't defined by escape sequence. For example, printEscaped("s") will print the same thing as printEscaped("\u0073"): they will both print \u0073. So you have to be careful to only call the method on strings where you are sure you want every character printed in "escape notation."

Upvotes: 0

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

Something like this?

public class Example {

    public static void main(String[] argv) {
        System.out.println("= First try =");
        System.out.println("\n\u0073");
        System.out.println("= Second try =");
        System.out.println("\n\\u0073");
    }

}

Which will output this:

= First try =

s
= Second try =

\u0073

Upvotes: 1

Jiri Kremser
Jiri Kremser

Reputation: 12857

You may use the StringEscapeUtils. There is method escapeJava() on it. Unfortunately, imo, there is no way to escape unicode literals like \u0073 so for your example input "\"\n\u0073\"", StringEscapeUtils.escapeJava("\"\n\u0073\"") will return \"\ns\"

Upvotes: 3

Related Questions