EqualsIgnoreCase()无法正常工作.

Jok*_*ker 8 java string locale case-sensitive

当我运行以下程序时,它只打印

equals says they are equal
Run Code Online (Sandbox Code Playgroud)

但是从java 8中的equalsIgnoreCase docs我们得到:

如果至少满足下列条件之一,则两个字符c1和c2被视为相同的忽略大小:
•将方法java.lang.Character.toUpperCase(char)应用于每个字符会产生相同的结果

    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");

    }
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是为什么这个程序不打印

equalsIgnoreCase says they are equal
Run Code Online (Sandbox Code Playgroud)

与在两个操作中一样,使用大写字符.

ΦXo*_*a ツ 10

你正在使用/比较德国ß标志,它的大写产生SS ...所以你需要使用Locale.German

if (string1.toUpperCase(Locale.GERMAN).equals(string2.toUpperCase(Locale.GERMAN)))
Run Code Online (Sandbox Code Playgroud)

这将返回真实....

  • 更确切地说:"ß".toUpperCase()=="SS",而Character.toUpperCase('ß')=='ß' (6认同)
  • FunFact:在德国,如果用大写字母书写,则小写字母s"ß"变为"SZ". (2认同)
  • @Korashen:那是错的.自2006年以来,以大写字母替换ß只是SS.在此之前,可以使用bot SS和SZ (2认同)