Java實(shí)現(xiàn)中文字符串與unicode互轉(zhuǎn)工具類
本文實(shí)例為大家分享了Java實(shí)現(xiàn)中文字符串與unicode互轉(zhuǎn)的具體代碼,供大家參考,具體內(nèi)容如下
原理利用了java實(shí)現(xiàn)js的escape以及unescape函數(shù)。
/**
* 中文字符串和unicode互轉(zhuǎn)工具類 <br>
*
* @author hkb <br>
*/
public class UnicodeConvertUtils {
/**
* 實(shí)現(xiàn)js的escape函數(shù)
*
* @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();
}
/**
* 實(shí)現(xiàn)js的unescape函數(shù)
*
* @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轉(zhuǎn)中文
*
* @param input
* 待傳入字符串
* @return
*/
public static String toGb2312(String input) {
input = input.trim().replaceAll("(?i)\\\\u", "%u");
return unescape(input);
}
/**
* 中文字符串轉(zhuǎn)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"));
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基于ArrayList實(shí)現(xiàn)群主發(fā)紅包功能
這篇文章主要介紹了Java基于ArrayList實(shí)現(xiàn)群主發(fā)紅包功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Java多線程面試題之交替輸出問題的實(shí)現(xiàn)
本文主要介紹了Java多線程面試題之交替輸出問題的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
java的SimpleDateFormat線程不安全的幾種解決方案
但我們知道SimpleDateFormat是線程不安全的,處理時要特別小心,要加鎖或者不能定義為static,要在方法內(nèi)new出對象,再進(jìn)行格式化,本文就介紹了幾種方法,感興趣的可以了解一下2021-08-08
Java超詳細(xì)分析講解final關(guān)鍵字的用法
關(guān)于final關(guān)鍵字,它也是我們一個經(jīng)常用的關(guān)鍵字,可以修飾在類上、或者修飾在變量、方法上,以此看來定義它的一些不可變性!像我們經(jīng)常使用的String類中,它便是final來修飾的類,并且它的字符數(shù)組也是被final所修飾的。但是一些final的一些細(xì)節(jié)你真的了解過嗎2022-06-06
高并發(fā)系統(tǒng)的限流詳解及實(shí)現(xiàn)
這篇文章主要介紹了高并發(fā)系統(tǒng)的限流詳解及實(shí)現(xiàn),內(nèi)容詳細(xì),小編覺得很不錯,這里分享給大家,供需要的朋友參考。隨小編一起看看吧。2017-11-11
Java實(shí)現(xiàn)簡單圖形界面計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單圖形界面計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

