Table of contents
1.
Introduction
2.
What is equalsIgnoreCase() Method in Java?
3.
Syntax of Java equalsIgnoreCase() Method 
4.
Parameters of Java equalsIgnoreCase() Method 
5.
Return Value of qualsIgnoreCase() in Java
6.
Signature of Java qualsIgnoreCase()
7.
Internal Implementation of the equalsIgnoreCase() Method
7.1.
Java
8.
Use of Java equalsIgnoreCase()
8.1.
Java
9.
Example of Java String equalsIgnoreCase() Method
9.1.
Example 1: Simple Case-Insensitive Comparison
9.2.
Java
9.3.
Example 2: Comparison with Different Words
9.4.
Java
9.5.
Example 3: Comparing Null Strings (with Safe Check)
9.6.
Java
10.
Frequently Asked Questions
10.1.
Is Java equalsIgnoreCase null safe?
10.2.
What is the difference between string equals and string equalsIgnoreCase?
10.3.
What does equalsIgnoreCase do in Java?
10.4.
What is the difference between str1.equalsIgnoreCase("abc") and "abc".equalsIgnoreCase(str1), where str1 is a String?
11.
Conclusion
Last Updated: Jan 9, 2025
Easy

Java String equalsIgnoreCase() Method

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

String comparison is a common task in Java programming, whether you're building applications, processing user inputs, or managing data. One challenge often encountered is comparing strings without worrying about letter casing, as user inputs or data sources may use varying capitalization. Java’s equalsIgnoreCase() method offers a simple yet effective solution, allowing you to compare two strings while ignoring case differences.

In this blog, we'll explore the equalsIgnoreCase() method in detail, covering how it works, its syntax, and some practical examples to help you understand when and where to use it.

Java String equalsIgnoreCase() Method

What is equalsIgnoreCase() Method in Java?

As the name suggests, the equaleIgnoreCase () function checks if two strings are equal or not by ignoring their case. This means that irrespective of whether the characters are in upper case or lower case if the letters match, then the function returns true. If any letter in both the strings does not match at all, it returns false.

Syntax of Java equalsIgnoreCase() Method 

str2.equalsIgnoreCase(str1);

Parameters of Java equalsIgnoreCase() Method 

str1, str2 - the strings to be checked for equality.

Return Value of qualsIgnoreCase() in Java

boolean

Signature of Java qualsIgnoreCase()

public boolean equalsIgnoreCase(String str)  

 

Where str is the string to be compared with.

Internal Implementation of the equalsIgnoreCase() Method

  • Java

Java

import java.lang.*;
class Main 
{
public static void main(String[] args) 
{
String s = "HELLO";
String t = "hELlo";
int flag=0;
if (s.length()==t.length())
{
for(int i=0;i<s.length();i++)
{
if (s.charAt(i)!=t.charAt(i))
{
if (s.substring(i,i+1).equals(t.substring(i,i+1).toUpperCase()) || s.substring(i,i+1).equals(t.substring(i,i+1).toLowerCase()))
flag=1;
else flag=0;
}
else flag=1;
}
}
else flag=0;
if (flag==0)
System.out.println("Not Equal");
else
System.out.println("Yes Equal");

}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Yes Equal

 

Explanation

The above code sample shows how to check if one string is equal to another after ignoring their case, without using the equalsIgnoreCase() method. First, it checks if the length of the two strings is equal. Next, it checks both the strings character by character. If the characters are the same but in a different case, it converts one of the string’s characters into another case to check the equality. If none of the above conditions is satisfied then the strings are said to be unequal even after ignoring their case.

Use of Java equalsIgnoreCase()

The entire code shown above can be replaced with the equalsIgnoreCase() method.

  • Java

Java

class Main 
{
public static void main(String[] args)
{
String str1 = "HellO WORld";
String str2 = "hello world";
String str3 = "HELLO WORLD";
String str4 = "hello";
Boolean result;

// comparing str1 with str2
result = str1.equalsIgnoreCase(str2);
System.out.println(result);

// comparing str1 with str3
result = str1.equalsIgnoreCase(str3);
System.out.println(result);

// comparing str2 with str4
result = str2.equalsIgnoreCase(str4);
System.out.println(result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

true
true
false

 

Explanation

The code shown above defines four different strings - str1, str2, str3 and str4. Comparing str1, str2 and str3 with the equaleIgnoreCase() method returns true as they read the same word after ignoring the case of the individual letters. The fourth string is entirely different from the other three strings as it lacks a few characters, hence the use of equalsIgnoreCase on str4 and any of the three other strings results in false.

Example of Java String equalsIgnoreCase() Method

Example 1: Simple Case-Insensitive Comparison

  • Java

Java

public class EqualsIgnoreCaseExample1 {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";

boolean result = str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Are the strings equal? true

Explanation: Although the casing is different, equalsIgnoreCase() ignores this and returns true because the contents are otherwise identical.

Example 2: Comparison with Different Words

  • Java

Java

public class EqualsIgnoreCaseExample2 {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "java programming";

boolean result = str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Are the strings equal? false

Explanation: Here, the strings differ not just in case but also in content, so equalsIgnoreCase() returns false.

Example 3: Comparing Null Strings (with Safe Check)

  • Java

Java

public class EqualsIgnoreCaseExample3 {
public static void main(String[] args) {
String str1 = "Rahul";
String str2 = null;

// Safe check for null before comparing
boolean result = str1 != null && str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Are the strings equal? false

Explanation: Since str2 is null, the expression str1.equalsIgnoreCase(str2) would throw a NullPointerException. By using a null check (str1 != null), we prevent this error and output false.

Frequently Asked Questions

Is Java equalsIgnoreCase null safe?

No, Java's equalsIgnoreCase() is not null-safe. If the string being compared is null, it will throw a NullPointerException. You should add null checks before using it.

What is the difference between string equals and string equalsIgnoreCase?

equals() is case-sensitive while equalsIgnoreCase() ignores case when comparing strings. For example, "Hello" equals "hello" returns false, but "Hello" equalsIgnoreCase "hello" returns true.

What does equalsIgnoreCase do in Java?

equalsIgnoreCase() compares two strings for content equality regardless of letter case. It converts both strings to the same case internally before comparing them character by character.

What is the difference between str1.equalsIgnoreCase("abc") and "abc".equalsIgnoreCase(str1), where str1 is a String?

Both the statements will return true if the value of str1 is “abc”. However, if the value of str1 is null, str1.equalsIgnoreCase("abc") will throw a NullPointerException while “abc”.equalsIgnoreCase(str1) will return false.

Conclusion

In this article, we have discussed the Java String equalsIgnoreCase() Method. The equalsIgnoreCase() method is a powerful tool in Java String handling that simplifies case-insensitive string comparisons. While it offers convenience for comparing text regardless of case, developers should remember to always implement null checks to avoid NullPointerException and consider performance implications when comparing large strings. Use equals() instead when case-sensitivity matters, and be cautious when comparing internationalized strings.

Check out our articles on:

Live masterclass