how can I convert unicode string to ASCII in java

前端 未结 1 970
广开言路
广开言路 2021-01-02 05:59

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 proper

相关标签:
1条回答
  • 2021-01-02 06:21

    use java.text.Normalizer class to convert from unicode to ascii. here is a sample code from the answer https://stackoverflow.com/a/2097224/931982

    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());
    
    0 讨论(0)
提交回复
热议问题