简体   繁体   English

Java转义转义序列

[英]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 示例:如果我有一个内容为“\\ n \\ u0073”的字符串,我需要以这样的方式对它们进行转义:如果我将它打印到命令行,我会看到

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 . 您可以使用StringEscapeUtils There is method escapeJava() on it. 它上面有方法escapeJava() 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\\" 不幸的是,imo,没有办法逃脱unicode文字,如\\ u0073所以为你的例子输入“\\”\\ n \\ u0073 \\“”StringEscapeUtils.escapeJava("\\"\\n\s\\"")将返回\\"\\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. 它100%工作......唯一的弱点是我对每个角色都有一个明确的例子。 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. 我不认为RegEx可以匹配像\s这样的字符定义,但我不确定。

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 . 例如, printEscaped("s")将打印与printEscaped("\s") :它们都将打印\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." 因此,您必须小心在字符串上调用方法,您确定要在“转义符号”中打印每个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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