STRING INSTANCE METHODS
Java String equalsIgnoreCase() method
equalsIgnoreCase() compares two String strings ignoring letter case. If the letter sequences match, it returns true; if the text differs, it returns false.
Comparing strings without considering case
String pirmas = "JavaQuest";
String antras = "javaquest";
String trecias = "JavaScript";
System.out.println(pirmas.equalsIgnoreCase(antras));
System.out.println(pirmas.equalsIgnoreCase(trecias));true
false
pirmasstores the text JavaQuest.antrasstores the same text in lowercase.equalsIgnoreCase()ignores case, so the first comparison returnstrue.treciashas a different character sequence, so the second comparison returnsfalse.
Why is equalsIgnoreCase() an instance method?
equalsIgnoreCase() is called on a specific string because it compares that text with another supplied string:
boolean sutampa = pirmas.equalsIgnoreCase(antras);
We therefore do not write String.equalsIgnoreCase(...). The left-hand pirmas object is one side of the comparison, and the text in parentheses is the other.
Method syntax
public boolean equalsIgnoreCase(String anotherString)
| Part | Meaning |
|---|---|
| Calling object | pirmas in the line pirmas.equalsIgnoreCase(antras) |
| Parameter | anotherString – the other string to compare |
| Parameter type | String |
| Return type | boolean |
true | The strings match ignoring case |
false | The strings differ or the argument is null |
| Error | NullPointerExceptionwhen the method is called on a null reference |
Using equalsIgnoreCase() in a condition
String ivestaRole = "ADMIN";
if (ivestaRole.equalsIgnoreCase("admin")) {
System.out.println("Atidaromas administratoriaus meniu");
} else {
System.out.println("Paprasto vartotojo meniu");
}Atidaromas administratoriaus meniu
Because equalsIgnoreCase() returns boolean, it is convenient to use directly in an if condition. This is useful when users may enter values with different case, for example, ADMIN, admin or Admin.
The difference from equals()
String miestas = "Vilnius";
String ivesta = "vilnius";
System.out.println(miestas.equals(ivesta));
System.out.println(miestas.equalsIgnoreCase(ivesta));false
true
equals() compares strings exactly and is case-sensitive. equalsIgnoreCase() treats Vilnius and vilnius as the same text because only letter case differs.
String lengths must match
String pirmas = "Java";
String antras = "JAVA!";
System.out.println(pirmas.equalsIgnoreCase(antras));false
Letter case does not matter, but character count still does. Java and JAVA! are not equal because the second has an extra exclamation mark.
A null argument returns false
String atsakymas = "taip";
String ivesta = null;
System.out.println(atsakymas.equalsIgnoreCase(ivesta));false
If the argument is null, equalsIgnoreCase() does not throw an error. That argument is not considered equal text, so the result is false.
The method cannot be called on null
String atsakymas = null;
System.out.println(atsakymas.equalsIgnoreCase("taip"));NullPointerException
The distinction matters: tekstas.equalsIgnoreCase(null) returns false, but null.equalsIgnoreCase("taip") is impossible because null is not an object and has no methods.
Safe comparison with a known value
String atsakymas = null;
if ("taip".equalsIgnoreCase(atsakymas)) {
System.out.println("Vartotojas sutiko");
} else {
System.out.println("Vartotojas nesutiko");
}Vartotojas nesutiko
If one value is a known string literal, it is safe to call the method on it. "taip".equalsIgnoreCase(atsakymas) works even when atsakymas is null.
Useful for short codes and choices
String kalba = "LT";
String ivesta = "lt";
System.out.println(kalba.equalsIgnoreCase(ivesta));true
equalsIgnoreCase() is particularly useful for short codes, command names, answers and choices where letter case should not change meaning. In this example, LT and lt represent the same language code.
Spaces are not ignored
String ivesta = " taip ";
System.out.println("taip".equalsIgnoreCase(ivesta));
System.out.println("taip".equalsIgnoreCase(ivesta.trim()));false
true
The method ignores only case, not extra spaces. If user input may have leading or trailing spaces, first use trim().
Common mistakes
- Expecting it to ignore spaces, punctuation or different characters.
- Forgetting that
equalsIgnoreCase()returnsfalsewhen the argument isnull. - Calling the method through a potentially
nullreference and getting aNullPointerException. - Confusing it with
equals(), which is case-sensitive. - Using it to search for a substring, where a better choice is
contains(),startsWith()orendsWith().
When should you use equalsIgnoreCase()?
equalsIgnoreCase() Use it to check whether two strings represent the same value when case does not matter: answers, commands, short codes, roles or simple settings. If case must match exactly, use equals(). To find a substring, use other String search methods.