unicode转成String

问题:有时我们用utf-8去接收,结果接收到的是unicode码,这时就需要将unicode转成string
列如:

\r\n\r\n2020-07-12 10:34:31<\/RES.1>\r\n0<\/RES.2>\r\n\u672A\u67E5\u8BE2\u5230\u6302\u53F7\u4FE1\u606F\u6216\u6302\u53F7\u4FE1\u606F\u5DF1\u8D85\u8FC7\u6709\u6548\u671F!<\/ERR>\r\n<\/RES>\r\n<\/MSG>

解决:编写一个方法将unicode码转成utf-8码
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* unicode转成string
* @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()) {
//group 6728
String group = matcher.group(2);
//ch:'木' 26408
ch = (char) Integer.parseInt(group, 16);
//group1 \u6728
String group1 = matcher.group(1);
str = str.replace(group1, ch + "");
}
return str;
}

转换结果:
2020-07-12 10:34:310未查询到挂号信息或挂号信息己超过有效期!

你可能感兴趣的:(unicode转成String)