String equalsIgnoreCase() Java

String equalsIgnoreCase() Java | The equalsIgnoreCase() is a built-in method in the Java string class that compares the two strings irrespective of their cases i.e. even if the string cases are different their cases will be ignored during the comparison.

It is the same as the equals() method of the String class. The equalsIgnoreCase() method is not case sensitive whereas the equals() method is case sensitive. The equals() method compare with the case but equalsIgnoreCase() compare ignoring the case.

Similar to the equals() method the equalsIgnoreCase() also has boolean return type. They return true if the strings are equal else false if the strings are not equal. Let us see the example for Java string equalsIgnoreCase() Method:-

Example-1:-
String1 = “REPEAT”
String2 = “repeat”
Result => True

Example-2:-
String1 = “null”
Strign2 = “nULl”
Result => True

Example-3:-
String1 = “bored”
String2 = “boring”
Result => False

Java String equalsIgnoreCase()

The method syntax of the equalsIgnoreCase() is as follows:- public boolean equalsIgnoreCase(String anotherString)

Parameter:- anotherString – The string which will be compared with this string.
Return value:- true if the argument is not null and it represents an equivalent String ignoring case; false otherwise

The method implementation of Java string equalsIgnoreCase() is given as follows:-

public boolean equalsIgnoreCase(String anotherString) {
   return (this == anotherString) ? true
      : (anotherString != null)
      && (anotherString.length() == length())
      && regionMatches(true, 0, anotherString, 0, length());
}

Example of String equalsIgnoreCase() In Java

Now let us see some programs to understand the String equalsIgnoreCase() method working principle. We will take multiple strings and compare them using the equalsIgnoreCase() method.

public class Main {
   public static void main(String[] args) {
      String str1 = "angry bird";
      String str2 = "ANGRY BIRD";
      String str3 = "bird angry";

      boolean res1 = str2.equalsIgnoreCase(str1);
      System.out.println("str2 is equal to str1 = " + res1);

      boolean res2 = str2.equalsIgnoreCase(str3);
      System.out.println("str2 is equal to str3 = " + res2);
   }
}

Output:-

str2 is equal to str1 = true
str2 is equal to str3 = false

With respect to content or data, str1 and str2 contain the same characters but one is in uppercase and one is in lowercase. Since the equalsIgnoreCase() method ignores the cases while comparison therefore it returns true. But the str2 and str3 are completely different from each other and hence equalsIgnoreCase() return false. Let us see one more example to understand it clearly.

public class Main {
   public static void main(String args[]) {
      String str1 = "hello world";
      String str2 = "Hello WORLD";
      String str3 = "HeLlO WOrlD";
      String str4 = "JAVA";
      
      System.out.println(str1.equalsIgnoreCase(str2));
      System.out.println(str1.equalsIgnoreCase(str3));
      System.out.println(str1.equalsIgnoreCase(str4));
   }
}

Output:-

true
true
false

equalsIgnoreCase() Method of String Java Example

We have a list of strings, and we want to check whether the list contains a given string (ignoring the case) or not? To check whether a list contains a given object or not contains() method is given, but it compares with the case. For string objects, if we want to compare by ignoring the case then we have to use the equalsIgnoreCase() method.

import java.util.ArrayList;
public class Main{
   public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<>();
      list.add("Happy");
      list.add("sad");
      list.add("angry");
      list.add("fear");
      
      String string1 = "ANGRY";
      System.out.println(list.contains(string1));
      
      boolean exist = false;
      for (String string : list) {
         if (string.equalsIgnoreCase(string1)) {
            exist = true;
            break;
         }
      }
      
      if(exist) {
       System.out.println("List contains given string");
      } else {
       System.out.println("List doesn't contain given string");
      }
   } 
}

Output:-

false
List contains given string

Apart from equals() and equalsIgnoreCase() method, Java String class also contains compareTo() and compareToIgnoreCase() methods. We can use of these method to compare two string objects based on our requirement.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *