【问题标题】:Replace Unicode \u texts in String with Java [duplicate]用Java替换字符串中的Unicode \u文本[重复]
【发布时间】:2018-10-03 01:17:52
【问题描述】:

我有这个示例StringCooking \u0026 Baking Needs \u002A 文本具有Unicode 用实际字符串替换所有Unicode 的最佳方法是什么?比如用&替换\0026等等。

这是我的初始代码:

public static String tidy(String s) {
    String unicodeCharRegex = "\\\\u[A-Fa-f\\d]{4}\n";
    if(s != null && !s.isEmpty()) {
        Pattern p = Pattern.compile(unicodeCharRegex);
        Matcher m = p.matcher(s);
        while(m.find()) {

        }
    }
    return s;
}

private String unicodeToString(String u) { 
    // TODO:
    return "";
}

【问题讨论】:

    标签: java regex


    【解决方案1】:

    您可以使用下面的code

    private String unicodeToString(String u) { 
    
    String str = u.split(" ")[0];
    str = str.replace("\\","");
    String[] arr = str.split("u");
    String text = "";
    for(int i = 1; i < arr.length; i++){
        int hexVal = Integer.parseInt(arr[i], 16);
        text += (char)hexVal;
    }
    
      return text;
    }
    

    【讨论】:

    • 你为什么只是从接受的重复答案中复制代码?
    • 谢谢它的工作。
    猜你喜欢
    • 2012-04-19
    • 1970-01-01
    • 2021-03-28
    • 2016-11-04
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多