public String unicode2Chinese(String utfString){
StringBuilder sb = new StringBuilder();
int i = -1;
int pos = 0;
while((i=utfString.indexOf("\\u", pos)) != -1){
sb.append(utfString.substring(pos, i));
if(i+5<utfString.length()){
pos = i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));
}
}
sb.append(utfString.substring(pos));
return sb.toString();
}
public boolean isChinese(char c){
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if(ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
||ub==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
||ub==Character.UnicodeBlock.GENERAL_PUNCTUATION
||ub==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
||ub==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
||ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)
{
return true;
}
return false;
}
public static String chinese2Unicode(String str){
String result="";
for(int i=0;i<str.length();i++){
int chr1 = (char) str.charAt(i);
if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)
result+="\\u" + Integer.toHexString(chr1);
}else{
result+=str.charAt(i);
}
}
return result;
}
StringBuilder sb = new StringBuilder();
int i = -1;
int pos = 0;
while((i=utfString.indexOf("\\u", pos)) != -1){
sb.append(utfString.substring(pos, i));
if(i+5<utfString.length()){
pos = i+6;
sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));
}
}
sb.append(utfString.substring(pos));
return sb.toString();
}
public boolean isChinese(char c){
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if(ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
||ub==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
||ub==Character.UnicodeBlock.GENERAL_PUNCTUATION
||ub==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
||ub==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
||ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)
{
return true;
}
return false;
}
public static String chinese2Unicode(String str){
String result="";
for(int i=0;i<str.length();i++){
int chr1 = (char) str.charAt(i);
if(chr1>=19968&&chr1<=171941){//汉字范围 \u4e00-\u9fa5 (中文)
result+="\\u" + Integer.toHexString(chr1);
}else{
result+=str.charAt(i);
}
}
return result;
}