Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's equalsIgnoreCase fails with ß ("Sharp S" used in German alphabet)

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?

like image 554
targumon Avatar asked Aug 26 '09 11:08

targumon


1 Answers

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.

like image 64
Aaron Digulla Avatar answered Oct 12 '22 23:10

Aaron Digulla