日期:2014-05-20 浏览次数:21008 次
public static String UnicodeToString(String str) {
Pattern pattern = null;
if (str.startsWith("&#")) {
pattern = Pattern.compile("(?:&#(\\d{4,5});?)");
} else if (str.startsWith("\\u")) {
pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
} else {
throw new RuntimeException("Unicode Not Support");
}
char ch;
String ret = "";
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
if (matcher.groupCount() > 1) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
} else {
ch = (char) Integer.parseInt(matcher.group(1), 10);
}
ret += ch;
}
return ret;
}
------解决方案--------------------
测试成功,楼主结贴给分吧
String str = "跳舞鞋红色亲子鞋215BRAND:足下有福";
Pattern pattern = Pattern.compile("(&#(\\p{XDigit}{5});)");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2));
str = str.replace(matcher.group(1), ch + "");
}
System.out.println(str);
//输出结果为:跳舞鞋红色亲子鞋215BRAND:足下有福
------解决方案--------------------
ldh911 大神始终快我一些啊
另外unicode 是把字符编译为16进制的 类似
楼主要处理的字符串其实不是普通的unicode编码 而是&# + ascii +";" 的html实体符号
中文“足” 的unicode为 “\u8db3” 而楼主的是 “足”
也就是16进制和10进制的区别
------解决方案--------------------
足 这种是html entity中的decimal character reference
也可以利用现成的jakarta commons lang包
比如commons-lang-2.6.jar
import org.apache.commons.lang.StringEscapeUtils;
public class Test {
public static void main(String[] args) {
System.out.println(StringEscapeUtils.unescapeHtml("足"));
System.out.println(StringEscapeUtils.unescapeHtml(""));
}
}
------解决方案--------------------
alert(String.fromCharCode(36275,19979,26377,31119));