How to compare non english characters with accents

不羁岁月 提交于 2019-11-29 13:11:05

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.

Alexis King

To do this you can use Java's Normalizer class. Just normalize the Strings, then strip out the diacritical marks, like so:

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

You can then use this to compare the two strings minus the accents:

stripAccents(string1).equals(stripAccents(string2))
if (string1 != null){
if (string1.equals(string2)){
System.out.println("Equal");
}
else{
System.out.println("Not Equal");
}

There is a way to compare 2 strings values in java.

        if(String1.equals(String2))
        {
           System.out.println("Equal");
        }
        else
        {
           System.out.println("Not equal");
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!