Java - String equalsIgnoreCase() Method



Description

The java.lang.String.equalsIgnoreCase() method compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Declaration

Following is the declaration for java.lang.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.

Exception

NA

Comparing Strings in Case Insensitive Way Example

The following example shows the usage of java.lang.String.equalsIgnoreCase() method. In the following program, we are instating the String class with the value "sachin tendulkar", "amrood admin" and "AMROOD ADMIN". Using the equalsIgnoreCase() method, we are trying to determine whether the given strings are equal or not in case insensitive way.

package com.tutorialspoint;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "sachin tendulkar";
      String str2 = "amrood admin";
      String str3 = "AMROOD ADMIN";
      
      // checking for equality with case ignored
      boolean retval1 = str2.equalsIgnoreCase(str1);
      boolean retval2 = str2.equalsIgnoreCase(str3);
  
      // prints the return value
      System.out.println("str2 is equal to str1 = " + retval1);
      System.out.println("str2 is equal to str3 = " + retval2);        
   }
}

Let us compile and run the above program, this will produce the following result −

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

Checking String Object with Same String Literal Example

If the given string value is the same as the specified object value, the equalsIgnoreCase() method returns true.

In the following program, we are instating the String class with the value "Java". Using the equals() method, we are trying to determine whether the given string is equal to the specified object "Java" or not.

package com.tutorialspoint;

public class Equal {
   public static void main(String[] args) {
      
      //instantiate a String class
      String str = new String("Java");
      
      //initialize the string object
      String obj = "java";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equalsIgnoreCase() method
      System.out.println("The given string is equal to the specified object or not? " + str.equalsIgnoreCase(obj));
   }
}

Output

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

The given string is: Java
The object is: java
The given string is equal to the specified object or not? true

Checking String Object with Different String Literal Example

If the given string value is the different from the specified object value, the equalsIgnoreCase() method returns false.

In the following example, we are creating a string literal with the value "HelloWorld". Using the equalsIgnoreCase() method, we are trying to determine whether the given string is equal to the specified object "Hello" or not.

package com.tutorialspoint;

public class Equal {
   public static void main(String[] args) {
      
      //create a String literal
      String str = "HelloWorld";
      
      //initialize the string object
      String obj = "Hello";
      System.out.println("The given string is: " + str);
      System.out.println("The object is: " + obj);
      
      //using the equalsIgnoreCase() method
      System.out.println("The given string is equal to the specified object or not? " + str.equalsIgnoreCase(obj));
   }
}

Output

Following is the output of the above program −

The given string is: HelloWorld
The object is: Hello
The given string is equal to the specified object or not? false
java_lang_string.htm
Advertisements