Java’s equalsIgnoreCase fails with ß (“Sharp S” used in German alphabet)

internationalizationjavalocalelocalization

my first question here 🙂
Did my best reading the rules and searching if the question was already asked before.

The following code

    String[] strings = {"cAsE", "\u00df"};
    for (String str : strings) {
        System.out.println(str.equalsIgnoreCase(str.toLowerCase()));
        System.out.println(str.equalsIgnoreCase(str.toUpperCase()));
    }

outputs true 3 times (cAsE = case; cAsE = CASE; ß = ß)
but also 1 false (ß != SS).
Tried using toLowerCase(Locale) but it did't help.

Is this a known issue?

Best Solution

Until recently, Unicode didn't define an uppercase version of s-sharp. I'm not sure whether the latest Java 7 version does already include this new character and whether it handles it correctly. I suggest to give it a try.

The reason why str.toLowerCase() doesn't return the same as str.toUpperCase().toLowerCase() is that Java replaces ß with SS but there is no way to go back, so SS becomes ss and the compare fails.

So if you need to level the case, you must use str.toLowerCase(). If not, then simply calling equalsIgnoreCase() without any upper/lower conversion should work, too.

Related Question