EqualsIgnoreCase () does not work as intended.

When I run the following program, it only prints

equals says they are equal 

However, from equalsIgnoreCase docs in java 8 we have:

Two characters c1 and c2 are considered the same ignoring case if one of the following conditions is true:
โ€ข Applying the java.lang.Character.toUpperCase (char) method to each character gives 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 does not print

 equalsIgnoreCase says they are equal 

As in both operations, uppercase characters are used.

+8
java string case-sensitive locale
source share
2 answers

You use / compare the German รŸ sign, its capital letters SS ... so you need to use Locale.German

 if (string1.toUpperCase(Locale.GERMAN).equals(string2.toUpperCase(Locale.GERMAN))) 

which will return true ....

+10
source share

Yes, that's right.

if (string1.equalsIgnoreCase (string2)) ....

ignores the lower and upper case of string1 and string2.

if (string1.equals (string2)) ....

discovers that different letters exist and are not printed. They are equal. The second example with an uppercase conversion is also OK.

0
source share

All Articles