5

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?

2
  • 4
    If you have the string literal "\n\u0073" in your code, it's too late, because at runtime there is no escape sequence. At runtime, that string is a newline character followed by a lowercase s. Commented Dec 12, 2012 at 0:23
  • You need to manually map them. Java does not know that you want an "\n" when it sees a '\u0010' character. Commented Dec 12, 2012 at 0:24

3 Answers 3

3

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\"

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.