Java String equalsIgnoreCase() method – Case-insensitive comparison

Java String equalsIgnoreCase() method is used to compare a string with the method argument object, ignoring case considerations.

In equalsIgnoreCase() method, two strings are considered equal if they are of the same length and corresponding characters in the two strings are equal ignoring case.

1. Java String equalsIgnoreCase() method

/**
* @param  anObject - The object to compare
* @return true -  if the non-null argument string represents the same sequence of characters to this string
*         false - in all other cases       
*/
public boolean equalsIgnoreCase(String anotherString) {
    return (this == anotherString) ? true
            : (anotherString != null)
            && (anotherString.value.length == value.length)
            && regionMatches(true, 0, anotherString, 0, value.length);
}
  1. Java equalsIgnoreCase() method is used to check equal strings in case-insensitive manner.
  2. Do not use '==' operator. It checks the object references, which is not not desirable in most cases.
  3. Passing ‘null’ to method is allowed. It will return false.

2. Java String equalsIgnoreCase() example

Java program to check if two strings are equal (case-insensitive). Notice that equals() and equalsIgnoreCase() methods behave in same way, except later is case-insensitive.

public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        String authorName = "Lokesh gupta";

        //1 - case-insensitive comparison
        isEqualString = blogName.equalsIgnoreCase("HOWTODOINJAVA.COM");   //true

        //2 - case-insensitive comparison
        isEqualString = blogName.equalsIgnoreCase("howtodoinjava.com");   //true
        
        //3 - check two strings for same character sequence ignoring case
        boolean isEqualString = blogName.equalsIgnoreCase(authorName);    //false
        
        //4 - null is allowed
        isEqualString = blogName.equalsIgnoreCase(null);                  //false
        
        
    }
}

3. Java String equals() example

Java program to check if two strings are equal (case-sensitive) using equals() method.

public class Main 
{
    public static void main(String[] args) 
    {
        String blogName = "howtodoinjava.com";
        String authorName = "Lokesh gupta";
        
        //1 - check two strings for same character sequence
        boolean isEqualString = blogName.equals(authorName);    //false
        
        //2
        isEqualString = blogName.equals("howtodoinjava.com");   //true
        
        //3
        isEqualString = blogName.equals(null);                  //false
        
        //4 - case-sensitive
        isEqualString = blogName.equals("HOWTODOINJAVA.COM");   //false
    }
}

4. Difference between equals and equalsIgnoreCase

Clearly, the difference between equals and equalsIgnoreCase in Java is case-sensitity while performing the string comparisons.

  1. equals() method does case-sensitive comparison.
  2. equalsIgnoreCase() method does case-insensitive comparison.

Happy Learning !!

Reference: String Java Doc

Leave a Reply

2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial