有时候发现数据是unicode类型的,需要将相关unicode类型的数据转化为utf8类型才可以显示。
commons-lang3包里面就要这样子的功能,可以用StringEscapeUtils.unescapeJava(content)将对应的unicode编码的content转化为
utf-8的内容。
使用时发现相关方法已经过期了:
相关的类和方法移到commons-text-1.4.jar这个包里面了。
例子:
public static void main(String[] args) {
System.out.println("\\u6d59\\u6c5f");
System.out.println(StringEscapeUtils.unescapeJava("\\u6d59\\u6c5f"));
String helloUnicodeStr = "\\u4f60\\u597d";
System.out.println(helloUnicodeStr);
// helloUnicodeStr为unicode编码的字符串,helloStr为转码后的utf-8的字符串
String helloStr = StringEscapeUtils.unescapeJava(helloUnicodeStr);
System.out.println(helloStr);
// address为普通的utf-8的字符串,addressUnicodeStr为转化后对应的unicode编码的字符串
String address = "杭州";
String addressUnicodeStr = StringEscapeUtils.escapeJava(address);
System.out.println(addressUnicodeStr);
}
输出:
\u676d\u5dde
杭州
\u4f60\u597d
你好
\u676D\u5DDE