gpt4 book ai didi

java 。比较字符串时忽略重音符号

转载 作者:IT老高 更新时间:2023-10-28 20:28:55 25 4
gpt4 key购买 nike

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

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

返回相等的。

谢谢

最佳答案

我认为您应该使用 Collator类(class)。它允许您设置强度和语言环境,它会适本地比较字符。

来自 Java 1.6 API:

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 类肯定可以正确处理这个问题。

关于 java 。比较字符串时忽略重音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2373213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com