How to compare non english characters with accents

前端 未结 4 976
既然无缘
既然无缘 2020-12-20 11:41

I want to compare 2 strings which have some non English character in them

String1 = debarquer
String2 = débárquér

On comparing above 2 str

4条回答
  •  北海茫月
    2020-12-20 12:36

    Use the Collator class. It allows you to set a strength and locale and it will compare characters appropriately.

    It should be something similar to this (NOTE: I have not tested the program)

    import java.text.Collator;
    import java.util.Locale;
    
    public class CollatorExp {
    
        public static void main(String[] args) {
            Collator collator = Collator.getInstance(Locale.FRENCH);
            collator.setStrength(Collator.PRIMARY);
    
            if (collator.compare("débárquér", "debarquer") == 0) {
                System.out.println("Both Strings are equal");
            } else {
                System.out.println("Both Strings are not equal");
            }
        } 
    }
    

    UPDATE: A point to note is that "débárquér" and "debarquer" should never be considered as equal. But if you will be sorting them out, then you do not want them to be compared based on their ASCII value. Take for example "Joao" and "João": If you sort them out based on ASCII, you might get Joao, John, João. This is obviously not good. Using the collator class handles this correctly.

提交回复
热议问题