Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EqualsIgnoreCase() not working as intended.

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.

like image 834
Sachin Sachdeva Avatar asked May 29 '17 09:05

Sachin Sachdeva


People also ask

What is equalsIgnoreCase () in Java?

The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not.

What is the difference between equals () and equalsIgnoreCase ()?

Difference between equals() vs equalsIgnoreCase() in JavaUse equals() in Java to check for equality between two strings. Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.

How do you write an equalsIgnoreCase in Java?

equalsIgnoreCase(str1); Note: Here str1 and str2 both are the strings that we need to compare. Parameters: A string that is supposed to be compared. Return Type: A boolean value, true if the argument is not null and it represents an equivalent String ignoring case, else false.


1 Answers

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

like image 94
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 07 '22 20:10

ΦXocę 웃 Пepeúpa ツ