在HTML中,中文的“天”可以表示爲 “天”
一項目中需要轉換,對於這一次在服務端轉換會更方便擴展,所以再寫一個JAVA的版本出來了。
轉換過程
Javascript
function ConvUtf8(obj) {
return obj.replace(/[^\u0000-\u00FF]/g, function ($0) { return escape($0).replace(/(%u)(\w{4})/gi, "&#x$2;") });
}
JAVA
public static String UTF8_html_conv(String str){
StringBuffer stbPreemptionArg = new StringBuffer();
for(int i = 0;i<str.length();i++){
if (str.codePointAt(i) > 255){
stbPreemptionArg.append("&#x"+Integer.toString(str.charAt(i), 16)+";");
}else{
stbPreemptionArg.append(str.charAt(i));
}
}
return stbPreemptionArg.toString();
}