Java - String equalsIgnoreCase() Method



The Java String equalsIgnoreCase() method is, used to compare two Strings, ignoring case considerations. The two strings are considered equal ignoring cases if they are of the same length, and corresponding characters in the two strings are equal.

Lets assume that we've taken two string input values, "JAVA" (upper case) and "java" (lower case), and if we compare both of them using the equalIgnoreCase() method, the method will return true as output. Because the method ignores string cases when comparing them.

The equalIgnoreCase() method accepts a parameter as a string that holds the value of the string argument.

Syntax

Following is the syntax of the Java String equalsIgnoreCase() method −

public boolean equalsIgnoreCase(String anotherString)

Parameters

  • anotherString − This is the String to compare this String against.

Return Value

This method returns true if the argument is not null and it represents an equivalent String ignoring case, else false.

Example

If the current string is equal to the string argument, but the cases are different, the equalIgnoreCase() method returns true.

In the following program, we are creating a string literal with the value HELLO. Then, Using the equalIgnoreCase() method, we are trying to compare the current string with the argument string hello.Since the method ignores the case while comparing them, it returns true.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      // create a string literal
      String str1 = "HELLO";
      
      //initialize the argument string
      String str2 = "hello";
      System.out.println("The current string is: " + str1);
      System.out.println("The argument string is: " + str2);
      
      //using the equalIgonreCase() method
      System.out.println("The string is equal to the argument string or not: " + str1.equalsIgnoreCase(str2));
   }
}

Output

On executing the above program, it will produce the following result −

The current string is: HELLO
The argument string is: hello
The string is equal to the argument string or not: true

Example

If the current string is different from the argument string, this method returns false.

In the following example, we are creating an object of the string class with the value TutorialsPoint. Using the equalIgnoreCase() method, we are trying to compare the current string with the argument string Point. Since the current is different from the argument string, the method returns false.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str1 = new String("TutorialsPoint");
      
      //initialize the argument string
      String str2 = "Point";
      System.out.println("The current string is: " + str1);
      System.out.println("The argument string is: " + str2);
      
      //using the equalIgonreCase() method
      System.out.println("The string is equal to the argument string or not: " + str1.equalsIgnoreCase(str2));
   }
}

Output

Following is the output of the above program −

The current string is: TutorialsPoint
The argument string is: Point
The string is equal to the argument string or not: false

Example

Using the conditional statement to check whether the current string is equal to the argument string or not.

In the following program, we are instantiating the string class with the value Java. Then, we are creating a string literal java that holds the value of the argument string. Using the equalIgnoreCase() method, we are comparing them.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str1 = new String("Java");
      
      //initialize the argument string
      String str2 = "java";
      System.out.println("The current string is: " + str1);
      System.out.println("The argument string is: " + str2);
      
      //using the equalIgonreCase() method
      boolean bool = str1.equalsIgnoreCase(str2);
      System.out.println("The equalIgnoreCase() method returns: " + bool);
      if(bool) {
         System.out.println("The string is equal to the argument string");
      } else {
         System.out.println("The string is not equal to the argument string");
      }
   }
}

Output

The above program, produces the following output −

The current string is: Java
The argument string is: java
The equalIgnoreCase() method returns: true
The string is equal to the argument string

Example

If the string argument value is null, the equalIgnoreCase() method returns false.

In this program, we are creating a string literal with the value Hello. Using the equalIgnoreCase() method, we are trying to compare the current string with the argument string null.

import java.lang.*;
public class Equal {
   public static void main(String[] args) {
      try {
         
         // create a string literal
         String str1 = new String("Java");
         
         //initialize the argument string
         String str2 = null;
         System.out.println("The current string is: " + str1);
         System.out.println("The argument string is: " + str2);
         
         //using the equalIgonreCase() method
         boolean bool = str1.equalsIgnoreCase(str2);
         System.out.println("The equalIgnoreCase() method returns: " + bool);
         if(bool) {
            System.out.println("The string is equal to the argument string");
         } else {
            System.out.println("The string is not equal to the argument string");
         }
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the above program, it generates the following output −

The current string is: Java
The argument string is: null
The equalIgnoreCase() method returns: false
The string is not equal to the argument string
java_lang_string.htm
Advertisements