java unicode 工具类_Java实现中文字符串与unicode互转工具类

本文实例为大家分享了java实现中文字符串与unicode互转的具体代码,供大家参考,具体内容如下

原理利用了java实现js的escape以及unescape函数。

/**

* 中文字符串和unicode互转工具类

*

* @author hkb

*/

public class unicodeconvertutils {

/**

* 实现js的escape函数

*

* @param input

* 待传入字符串

* @return

*/

public static string escape(string input) {

int len = input.length();

int i;

char j;

stringbuffer result = new stringbuffer();

result.ensurecapacity(len * 6);

for (i = 0; i < len; i++) {

j = input.charat(i);

if (character.isdigit(j) || character.islowercase(j) || character.isuppercase(j)) {

result.append(j);

} else if (j < 256) {

result.append("%");

if (j < 16) {

result.append("0");

}

result.append(integer.tostring(j, 16));

} else {

result.append("%u");

result.append(integer.tostring(j, 16));

}

}

return result.tostring();

}

/**

* 实现js的unescape函数

*

* @param input

* 待传入字符串

* @return

*/

public static string unescape(string input) {

int len = input.length();

stringbuffer result = new stringbuffer();

result.ensurecapacity(len);

int lastpos = 0, pos = 0;

char ch;

while (lastpos < len) {

pos = input.indexof("%", lastpos);

if (pos == lastpos) {

if (input.charat(pos + 1) == 'u') {

ch = (char) integer.parseint(input.substring(pos + 2, pos + 6), 16);

result.append(ch);

lastpos = pos + 6;

} else {

ch = (char) integer.parseint(input.substring(pos + 1, pos + 3), 16);

result.append(ch);

lastpos = pos + 3;

}

} else {

if (pos == -1) {

result.append(input.substring(lastpos));

lastpos = len;

} else {

result.append(input.substring(lastpos, pos));

lastpos = pos;

}

}

}

return result.tostring();

}

/**

* unicode转中文

*

* @param input

* 待传入字符串

* @return

*/

public static string togb2312(string input) {

input = input.trim().replaceall("(?i)\\\\u", "%u");

return unescape(input);

}

/**

* 中文字符串转unicode

*

* @param input

* 待传入字符串

* @return

*/

public static string tounicode(string input) {

input = input.trim();

string output = escape(input).tolowercase().replace("%u", "\\u");

return output.replaceall("(?i)%7b", "{").replaceall("(?i)%7d", "}").replaceall("(?i)%3a", ":")

.replaceall("(?i)%2c", ",").replaceall("(?i)%27", "'").replaceall("(?i)%22", "\"")

.replaceall("(?i)%5b", "[").replaceall("(?i)%5d", "]").replaceall("(?i)%3d", "=")

.replaceall("(?i)%20", " ").replaceall("(?i)%3e", ">").replaceall("(?i)%3c", "

.replaceall("(?i)%3f", "?").replaceall("(?i)%5c", "\\");

}

/**

* 测试

*

* @param args

*/

public static void main(string[] args) {

system.out.println(tounicode("你好"));

system.out.println(togb2312("\u4f60\u597d"));

// 等同于上面

system.out.println(togb2312("\\u4f60\\u597d"));

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值