admin 管理员组文章数量: 887007
java字符串如何转uni,我怎么可以转换UNI code字符串在Java中为ASCII
I'm now trying to convert unicode font to ascii in android. I wrote following coding to convert unicode font to ascii but it's failed. Because result cannot display properly after being converted.
unicode font = 'ေနေကာင္းပါသလား' to something like '\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;
}
解决方案
use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer
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字符串如何转uni 我怎么可以转换UNI code字符串在Java中为ASCII
版权声明:本文标题:java字符串如何转uni,我怎么可以转换UNI code字符串在Java中为ASCII 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1732355463h1534305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论