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

前端 未结 8 1478
梦如初夏
梦如初夏 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:31

    If you still need that for Android API 8 or lower (Android 2.2, Java 1.5) where you don't have Normalizer class, here's my code, I think better to modify than Pentium10 answer:

    public class StringAccentRemover {
    
        @SuppressWarnings("serial")
        private static final HashMap accents  = new HashMap(){
            {
                put('Ą', 'A');
                put('Ę', 'E');
                put('Ć', 'C');
                put('Ł', 'L');
                put('Ń', 'N');
                put('Ó', 'O');
                put('Ś', 'S');
                put('Ż', 'Z');
                put('Ź', 'Z');
    
                put('ą', 'a');
                put('ę', 'e');
                put('ć', 'c');
                put('ł', 'l');
                put('ń', 'n');
                put('ó', 'o');
                put('ś', 's');
                put('ż', 'z');
                put('ź', 'z');
            }
        };
        /**
         * remove accented from a string and replace with ascii equivalent
         */
        public static String removeAccents(String s) {
            char[] result = s.toCharArray();
            for(int i=0; i

提交回复
热议问题