java将包含unicode的字符串转换成中文

\u5168\u7ad9\u63a5\u53e3\u5df2\u5347\u7ea7\u4e3a\u5206\u9875\u6a21\u5f0f\uff0c\u6bcf\u9875200\u6761\u6570\u636e\uff0c\u5206\u9875\u53c2\u6570\uff1a&page=1
转化成
全站接口已升级为分页模式,每页200条数据,分页参数:&page=1
代码如下

    public static void main(String[] args) {
        String r = "\u5168\u7ad9\u63a5\u53e3\u5df2\u5347\u7ea7\u4e3a\u5206\u9875\u6a21" + "\u5f0f\uff0c\u6bcf\u9875200\u6761\u6570\u636e\uff0c\u5206\u9875\u53c2" + "\u6570\uff1a&page";
        System.out.println(asciiToNative(r));
    }

    private static String asciiToNative(String asciicode) {
        String[] asciis = asciicode.split("\\\\u");
        String nativeValue = asciis[0];
        try {
            for (int i = 1; i < asciis.length; i++) {
                String code = asciis[i];
                nativeValue += (char) Integer.parseInt(code.substring(0, 4), 16);
                if (code.length() > 4) {
                    nativeValue += code.substring(4, code.length());
                }
            }
        } catch (NumberFormatException e) {
            return asciicode;
        }
        return nativeValue;
    }
posted @ 2021-08-21 23:32  黄河大道东  阅读(846)  评论(0编辑  收藏  举报