EqualsIgnoreCase()无法正常工作. [英] EqualsIgnoreCase() not working as intended.
问题描述
当我运行以下程序时,它只会打印
When i run the following program it prints only
equals says they are equal
但是从Java 8中的equalsIgnoreCase文档中,我们可以得到:
However From equalsIgnoreCase docs in java 8 we have :
如果将两个字符c1和c2视为相同,则忽略大小写至少满足以下条件之一:
•应用方法每个字符的java.lang.Character.toUpperCase(char)产生相同的结果
Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:
• Applying the method java.lang.Character.toUpperCase(char) to each character produces the same result
public class Test {
public static void main(String[] args) {
String string1 = "abc\u00DF";
String string2 = string1.toUpperCase();
if (string1.equalsIgnoreCase(string2))
System.out.println("equalsIgnoreCase says they are equal");
if (string1.toUpperCase().equals(string2.toUpperCase()))
System.out.println("equals says they are equal");
}
}
所以我的问题是为什么该程序无法打印
So my question is why this program is not printing
equalsIgnoreCase says they are equal
与两个操作一样,使用大写字母.
As in both operations upper case charcters are used.
推荐答案
您正在使用/比较德国ß符号,其大写字母生成 SS ...因此您需要使用 Locale.German
You are using/comparing the german ß sign, its uppercase produce SS... so you need to use the Locale.German
if (string1.toUpperCase(Locale.GERMAN).equals(string2.toUpperCase(Locale.GERMAN)))
将返回true....
that will return true....
这篇关于EqualsIgnoreCase()无法正常工作.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!