我有一个格式的字符串:



我想显示两个特殊字符,但为了能够做到这一点,我必须首先将字符串转换为这种格式:



我试过写这个,但它给出了一个编译错误:

String encodedInput = input.replace("\\u", "\u");

这必须是直截了当的事情,但我就是无法理解。有任何想法吗?

最佳答案

不幸的是,我不知道一种评估。

    String s = "aaa\\u2022bbb\\u2014ccc";
    StringBuffer buf = new StringBuffer();
    Matcher m = Pattern.compile("\\\\u([0-9A-Fa-f]{4})").matcher(s);
    while (m.find()) {
        try {
            int cp = Integer.parseInt(m.group(1), 16);
            m.appendReplacement(buf, "");
            buf.appendCodePoint(cp);
        } catch (NumberFormatException e) {
        }
    }
    m.appendTail(buf);
    s = buf.toString();

关于java - 如何在 Java 字符串中用\u 替换\\u,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10035891/

10-11 22:31
查看更多