How to compare two Strings using equalsIgnoreCase() Method in Java?

In the last tutorial, We have learned how to compare two strings using the equals() method. But there is one limitation of the equals() method. By default equals() method checks the content including the case also i.e. The comparison is case sensitive. If the comparison is case insensitive it returns false. To overcome this problem we can use the equalsIgnoreCase() method in Java. Let’s first see the example of, what the problem to use the equals() method.

class StringCompare{
public static void main(String args[]){
String str1 = "Javastudypoint";
String str2 = new String("JAVA");
String str3 = new String("Javastudypoint");
String str4 = "Java";
System.out.println(str1.equals(str3)); //true
System.out.println(str2.equals(str4)); //false
System.out.println(str1.equals(str2)); //false
}
}

Output:

true
false
false

The problem with the above code is it check the case also. In the above code, the str2 and the str3 have the same content but the case is different therefore the equals() method returns false.

equalsIgnoreCase() in Java Definition & Usage

To overcome the above problem, we can use the equalsIgnoreCase() method. This method is used to perform the comparison that ignores the case differences. When it compares two strings, it considered A-Z the same as a-z.

Syntax: The syntax of the equalsIgnoreCase() is given below:

public boolean equalsIgnoreCase()

Also, Read: How to compare two Strings in Java using equals() Method?

Pictorial Representation of Comparison of Strings using equalsIgnoreCase() Method

How to compare two Strings using equalsIgnoreCase() Method in Java

How to Compare two strings in Java Using the method equalsIgnoreCase()?

Let’s understand the equalsIgnoreCase() method in Java with an example.

class StringCompare{
public static void main(String args[]){
String str1 = "Javastudypoint";
String str2 = "javaSTUDYpoint";
String str3 = "JAVASTUDYPOINT";
String str4 = "Java Tutorial";
//true because content and case are same
System.out.println(str1.equalsIgnoreCase(str3));
//false because content and case are not the same.
System.out.println(str2.equalsIgnoreCase(str4));
//true because content and case are same
System.out.println(str1.equalsIgnoreCase(str2));
}
}

Output:

How to compare two Strings using equalsIgnoreCase() Method in Java

Example Illustrating Use of .equalsIgnoreCase in String Comparison

// Java program to demonstrate
// use of .equalsIgnoreCase operator in Java

class GFG {
public static void main(String[] args)
{

// Get some Strings to compare
String s1 = "A";
String s2 = "A";
String s3 = "a";
String s4 = new String("A");

// Compare s1 and s2
// It should return true as they both
// have the same content
System.out.println(s1 + " .equalsIgnoreCase " + s2
+ ": " + s1.equalsIgnoreCase(s2));

// Compare s1 and s3
// It should return true as they both
// have the same content being case insensitive
System.out.println(s1 + " .equalsIgnoreCase " + s3
+ ": " + s1.equalsIgnoreCase(s3));

// Compare s2 and s3
// It should return true as they both
// have the same content being case insensitive
System.out.println(s2 + " .equalsIgnoreCase " + s3
+ ": " + s2.equalsIgnoreCase(s3));

// Compare s1 and s4
// It should return true as they both
// have the same content
System.out.println(s1 + " .equalsIgnoreCase " + s4
+ ": " + s1.equalsIgnoreCase(s4));
}
}

Output:

A .equalsIgnoreCase A: true
A .equalsIgnoreCase a: true
A .equalsIgnoreCase a: true
A .equalsIgnoreCase A: true