Java.lang.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

Example

The following example shows the usage of java.lang.String.equalsIgnoreCase() method.

package com.tutorialspoint;

import java.lang.*;

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
java_lang_string.htm
Advertisements