Conversion from javascript-escaped Unicode to Java Unicode

无人久伴 提交于 2020-01-05 03:36:30

问题


I have a query string passed in through an HTTP request that has this character in it:

%u54E6

And I'd like to generate a string that contains the actual Chinese character so I can use it in a different part of the application, I've tried using this code:

String foo = "%u54E6";
String ufoo = new String(foo.replaceAll("%u([a-zA-Z0-9]{4})", "\\" + "u$1"));
System.out.println("ufoo: " + ufoo);

Unfortunately, all I'm getting is 'u54E6' printed to the console for the value, instead of the Chinese character.

Is there an easy way to convert the original string to a Unicode character in Java?


回答1:


You're trying to use \u escapes at run time. These are compile-time only. Instead, you should be able to do something like:

String foo = "%u54E6";
Pattern p = Pattern.compile("%u([a-zA-Z0-9]{4})");
Matcher m = p.matcher(foo);
StringBuffer sb = new StringBuffer();
while (m.find()) {
  m.appendReplacement(sb,
      String.valueOf((char) Integer.parseInt(m.group(1), 16)));
}
m.appendTail(sb);
System.out.println(sb.toString());


来源:https://stackoverflow.com/questions/1493576/conversion-from-javascript-escaped-unicode-to-java-unicode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!