Java语言对unicode转中文(unicode码之间混杂有数字和英文)
啥都不说了,直接上代码
public static String decodeUnicode(String unicode) {
List<String> list = new ArrayList<String>();
String reg= "\\\\u[0-9,a-f,A-F]{4}";
Pattern p = Pattern.compile(reg);
Matcher m=p.matcher(unicode);
while (m.find()){
list.add(m.group());
}
for (int i = 0, j = 2; i < list.size(); i++) {
String code = list.get(i).substring(j, j + 4);
char ch = (char) Integer.parseInt(code, 16);
unicode = unicode.replace(list.get(i),String.valueOf(ch));
}
return unicode;
}
public static void main(String[] args) throws Exception {
String str = "2002\\u5e7412\\u67081\\u65e5\\u96f6love\\u65f6\\uff0c\\u5f53\\u6b27\\u76df\\u5728\\u5e03\\u9c81\\u585e\\u5c14\\u738b";
System.out.println(decodeUnicode(str));
}
效果:
2002年12月1日零love时,当欧盟在布鲁塞尔王