Ignoring diacritic characters when comparing words with special characters (é, è, …)

前端 未结 8 1465
梦如初夏
梦如初夏 2021-02-05 18:08

I have a list with some Belgian cities with diacritic characters: (Liège, Quiévrain, Franière, etc.) and I would like to transform these special characters to compare with a lis

相关标签:
8条回答
  • 2021-02-05 18:42

    This is the simplest solution I've found so far and it works perfectly in our applications.

    Normalizer.normalize(string, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); 
    

    But I don't know if the Normalizer is available on the Android platform.

    0 讨论(0)
  • 2021-02-05 18:47

    As of Java 6, you can use java.text.Normalizer:

    public String unaccent(String s) {
        String normalized = Normalizer.normalize(s, Normalizer.Form.NFD);
        return normalized.replaceAll("[^\\p{ASCII}]", "");
    }
    

    Note that in Java 5 there is also a sun.text.Normalizer, but its use is strongly discouraged since it's part of Sun's proprietary API and has been removed in Java 6.

    0 讨论(0)
提交回复
热议问题