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));
Console output

true
false

  1. pirmas stores the text JavaQuest.
  2. antras stores the same text in lowercase.
  3. equalsIgnoreCase() ignores case, so the first comparison returns true.
  4. trecias has a different character sequence, so the second comparison returns false.

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)

Parts of the equalsIgnoreCase() method
PartMeaning
Calling objectpirmas in the line pirmas.equalsIgnoreCase(antras)
ParameteranotherString – the other string to compare
Parameter typeString
Return typeboolean
trueThe strings match ignoring case
falseThe strings differ or the argument is null
ErrorNullPointerExceptionwhen 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");
}
Console output

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));
Console output

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));
Console output

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));
Console output

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"));
Result

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");
}
Console output

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));
Console output

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()));
Console output

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() returns falsewhen the argument is null.
  • Calling the method through a potentially null reference and getting a NullPointerException.
  • Confusing it with equals(), which is case-sensitive.
  • Using it to search for a substring, where a better choice is contains(), startsWith() or endsWith().

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.