如何在Java中将Unicode字符串转换为ASCII
我现在正在尝试将unicode字体转换为android中的ascii。我写了下面的代码将unicode字体转换为ascii,但是失败了。因为转换后结果无法正确显示。
unicode字体=’ေနေကာင္းပါသလား’类似于’\ u100F \ u1039 \ u100D’
public static String toJAVA (String zawgyi) { String output = "";
char[] charArray = zawgyi.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char a = charArray[i];
if ((int) a > 255) {
output += "\\u" + Integer.toHexString((int) a) + "--";
} else {
output += a;
}
}
return output;
}
回答:
使用java.text.Normalizer类将unicode转换为ascii。这是答案的示例代码http://codingdict.com/questions/144863
String s = "口水雞 hello Ä";String s1 = Normalizer.normalize(s, Normalizer.Form.NFKD);
String regex = Pattern.quote("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
String s2 = new String(s1.replaceAll(regex, "").getBytes("ascii"), "ascii");
System.out.println(s2);
System.out.println(s.length() == s2.length());
以上是 如何在Java中将Unicode字符串转换为ASCII 的全部内容, 来源链接: utcz.com/qa/404357.html