Java。比较字符串时忽略重音符号 [英] Java. Ignore accents when comparing strings

查看:420
本文介绍了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:

您可以设置Collat​​or的力量
属性来确定
比较中显着的
差异的级别。四个优势是
提供:PRIMARY,SECONDARY,
TERTIARY和IDENTICAL。确切的
赋值语言的优点到
特性是依赖于语言环境的。对于
示例,在捷克语中,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.

这篇关于Java。比较字符串时忽略重音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆