简体   繁体   中英

Java escaping escape sequence

I want to escape escape sequences in a string.

Example: if I had a string whose contents were "\\n\s", 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?

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

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

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 \s , 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("\s") : they will both print \s . 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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM