8

When i run the following program it prints only

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 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.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
T-Bag
  • 10,916
  • 3
  • 54
  • 118

2 Answers2

11

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

that will return true....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 6
    To be more precise: "ß".toUpperCase() == "SS", while Character.toUpperCase('ß') == 'ß' – Florian Schaetz May 29 '17 at 09:19
  • Workaround: Don't use equalsIgnoreCase if you have locale-dependant Strings and can't be sure which Locale it is... – Florian Schaetz May 29 '17 at 09:21
  • 2
    FunFact: In Germany, the lower case sharp s "ß" becomes "SZ" if written in upper case. – Korashen May 29 '17 at 09:23
  • Those silently local aware methods are a terrible Java nightmare, I hate the default values in these. – Christophe Roussy May 29 '17 at 09:26
  • Just imagine the pain in the neck for languages like Turkish where they have vowels like: ***I, İ, ı, i*** – ΦXocę 웃 Пepeúpa ツ May 29 '17 at 09:30
  • 3
    @Korashen: that's wrong. since 2006, the replacement for ß in uppercase is only SS. Before that it was possible to use bot SS and SZ – P.J.Meisch May 29 '17 at 12:01
  • @P.J.Meisch Really? Well, it's a while since I went to school and they do like to change spelling every now and then... Yet you can still find the usage of SZ today. Guess there are more people who stick to the old(er) versions. – Korashen May 30 '17 at 14:53
  • 1
    @Korashen update: from now on, there is a special uppercase for 'ß' (http://www.rechtschreibrat.com/DOX/rfdr_PM_2017-06-29_Aktualisierung_Regelwerk.pdf). I wonder, how to produce that on a normal german keyboard, where shift-ß is mapped to `?`. – P.J.Meisch Jun 29 '17 at 15:05
  • @P.J.Meisch that depends on your OS. On Linux it's `alt gr` + `shift` + `s` – duckstep Jul 12 '17 at 22:22
0

Yes, its correct.

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

ignores the lower and uppercase of string1 and string2.

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

will detect, that there are different letters and does not print ..they are equal. Your second example with the uppercase conversion is OK too.

Norbert
  • 234
  • 1
  • 9