爪哇.比较字符串时忽略重音符号

2022-01-25 00:00:00 string compare java

问题很简单.JAVA中是否有任何函数可以比较两个字符串并忽略重音字符返回true?

The problem it's easy. Is there any function in JAVA to compare two Strings and return true ignoring the accented chars?

String x = "Joao";
String y = "João";

返回相等的.

谢谢

推荐答案

我认为你应该使用 Collat​​or 类.它允许您设置强度和语言环境,它会适当地比较字符.

I think you should be using the Collator class. It allows you to set a strength and locale and it will compare characters appropriately.

来自 Java 1.6 API:

From the Java 1.6 API:

您可以设置整理者的强度属性来确定水平差异被认为是显着的比较.四大实力是提供:小学、中学、第三和相同的.最正确语言优势分配功能取决于语言环境.为了例如,在捷克语中,e"和f"是考虑主要差异,而e"和ě"是次要区别,e"和E"是三级差异和 "e" 和 "e" 是相同的.

You can set a Collator's strength property to determine the level of difference considered significant in comparisons. Four strengths are provided: PRIMARY, SECONDARY, TERTIARY, and IDENTICAL. The exact assignment of strengths to language features is locale dependant. For example, in Czech, "e" and "f" are considered primary differences, while "e" and "ě" are secondary differences, "e" and "E" are tertiary differences and "e" and "e" are identical.

我认为这里的重点(人们试图提出的)是Joao"和João"永远不应该被认为是平等的,但是如果你正在做排序你不希望他们根据他们的 ASCII 值,因为那样你会得到像 Joao、John、João 这样的东西,这不好.使用 collat​​or 类肯定可以正确处理这个问题.

I think the important point here (which people are trying to make) is that "Joao"and "João" should never be considered as equal, but if you are doing sorting you don't want them to be compared based on their ASCII value because then you would have something like Joao, John, João, which is not good. Using the collator class definitely handles this correctly.

相关文章