java 将字符串中含有 unicode 转成中文
                        原创
                        
                        java
                    
                调用第三方 API 的时候,返回结果中把中文转成了 Unicode 的方式,我需要拿结果去展示,当然不能直接展示 Unicode 码。
然后写了一个小工具,把字符串中包含的中文 Unicode 码转换成中文显示。
package com.daimafans.unit;
import org.junit.Test;
public class UnicodeTest
{
    private String ascii2native(String asciicode)
    {
        String[] asciis = asciicode.split("\\\\u");
        StringBuilder nativeValue = new StringBuilder(asciis[0]);
        try
        {
            for (int i = 1; i < asciis.length; i++)
            {
                String code = asciis[i];
                nativeValue.append((char) Integer.parseInt(code.substring(0, 4), 16));
                if (code.length() > 4)
                {
                    nativeValue.append(code.substring(4, code.length()));
                }
            }
        }
        catch (NumberFormatException e)
        {
            return asciicode;
        }
        return nativeValue.toString();
    }
    @Test
    public void testUnicode()
    {
        String unicode = "who are you? \\u4ee3\\u7801\\u996dwww.daimafans.COM";
        System.out.println("" + ascii2native(unicode));
    }
}
测试结果正常!
