String str="\u4e0a\u6d77" 怎么把str转成中文?
4条回答 默认 最新
- xing23777 2011-08-02 06:46关注
[code="java"]
/**043 * unicode 转换成 中文
044 * @author fanhui
045 * 2007-3-15
046 * @param theString
047 * @return
048 */
049 public static String decodeUnicode(String theString) {
050 char aChar;
051 int len = theString.length();
052 StringBuffer outBuffer = new StringBuffer(len);
053 for (int x = 0; x < len;) {
054 aChar = theString.charAt(x++);
055 if (aChar == '\') {
056 aChar = theString.charAt(x++);
057 if (aChar == 'u') {
058 // Read the xxxx
059 int value = 0;
060 for (int i = 0; i < 4; i++) {
061 aChar = theString.charAt(x++);
062 switch (aChar) {
063 case '0':
064 case '1':
065 case '2':
066 case '3':
067 case '4':
068 case '5':
case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: throw new IllegalArgumentException( "Malformed \\uxxxx encoding."); } } outBuffer.append((char) value); } else { if (aChar == 't') aChar = '\t'; else if (aChar == 'r') aChar = '\r'; else if (aChar == 'n') aChar = '\n'; else if (aChar == 'f') aChar = '\f'; outBuffer.append(aChar); } } else outBuffer.append(aChar); } return outBuffer.toString(); }
[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报