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

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

本文介绍了如何比较非英语字符与重音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较其中有两个非英文字符的字符串

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

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

在比较2个以上的字符串时,他们应该说相等。

On comparing above 2 strings, they should say equal.

推荐答案

使用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.

这篇关于如何比较非英语字符与重音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:49