1

我正在使用 Jre 1.6。我正在执行以下代码行:

字符串 unicodeValue = "\u001B"; text = text.replaceAll("" + character, unicodeValue);

此处,文本是一个字符串对象,其中包含 Unicode 值“\u001B”的无效 XML 字符。因此,我将无效的 XML 字符转换为其 Unicode 值以写入 XML。

但是在执行 text.replaceAll 时,“\”被剥离,字符被“u001B”替换。

任何人都可以建议一种在用其 unicode 值替换字符后保留 '\' 的方法吗?

4

3 回答 3

2

问题是str.replaceAll(regex, repl)被定义为返回相同的

Pattern.compile(regex).matcher(str).replaceAll(repl)

但是文档replaceAll说,

请注意,替换字符串中的反斜杠 () 和美元符号 ($) 可能会导致结果与将其视为文字替换字符串时的结果不同。如上所述,美元符号可以被视为对捕获的子序列的引用,并且反斜杠用于转义替换字符串中的文字字符。

所以这意味着我们需要添加几个额外的转义层:

public class Foo {

    public static void main(String[] args)
    {
        String unicodeValue = "\u001B";
        String escapedUnicodevalue = "\\\\u001B";
        String text = "invalid" + unicodeValue + "string";

        text = text.replaceAll(unicodeValue, escapedUnicodevalue);

        System.out.println(text);
    }
}

根据需要打印invalid\u001Bstring

于 2013-01-31T05:17:45.763 回答
0

使用双斜杠\\表示转义\

String unicodeValue = "\\u001B"; text = text.replaceAll("" + character, unicodeValue);
于 2013-01-31T05:18:16.497 回答
0

这运行得很完美。我测试了它。

    char character = 0x1b;
    String unicodeValue = "\\\\u001B"; 
    String text = "invalid " + character + " string";
    System.out.println(text);
    text = text.replaceAll("" + character, unicodeValue);
    System.out.println(text);

刚刚使用了RegEx的概念。

于 2013-01-31T05:49:06.137 回答