Friday, February 13, 2009

java unicode字符串转换

/**
* 转换unicode字符串为其对应的实际字符串, UnicodeToString("测试\\u4E2D\\u6587") 输出为: "测试中文"
*
* @param str
* @return
*/
public static String UnicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
str = str.replace(matcher.group(1), ch + "");
}
return str;
}

Reference:
http://yuweijun.blogspot.com/2008/06/unicode.html
http://yuweijun.blogspot.com/2008/08/unicode-and-html-entities-in-javascript.html
http://yuweijun.blogspot.com/2008/12/rubyunicodeutf8.html

No comments :