且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何比较非英语字符与重音符号

更新时间:2023-02-26 09:25:07

使用Collat​​or类。它允许你设置一个强度和区域设置,它会适当地比较字符。

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");
        }
    } 
}

更新:值得注意的是,débárquér和debarquer永远不应被视为平等。但是如果你要对它们进行排序,那么你不希望它们根据它们的ASCII值进行比较。以Joao和João为例:如果你根据ASCII对它们进行排序,你可能会得到Joao,John,João。这显然不太好。使用collat​​or类可以正确处理。

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.