Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaJava String equalsIgnoreCase() Method with Examples

Java String equalsIgnoreCase() Method with Examples

In Java, equalsIgnoreCase() method of the String class compares two strings irrespective of the case (lower or upper) of the string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring case, else false.

Syntax of equalsIgnoreCase()

str2.equalsIgnoreCase(str1);

Parameters

  • str1: A string that is supposed to be compared.

Return Value

  • A boolean value that is true if the argument is not null and it represents an equivalent String ignoring case, else false.

Illustration

Input : str1 = "pAwAn";
        str2 = "PAWan"
        str2.equalsIgnoreCase(str1);
Output :true

Input : str1 = "powAn";
        str2 = "PAWan"
        str2.equalsIgnoreCase(str1);
Output :false
Explanation: powan and pawan are different strings. 

Note: str1 is a string that needs to be compared with str2.

Internal Implementation of the equalsIgnoreCase() Method

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

Example of String equalsIgnoreCase() Method

Java




// Java Program to Illustrate equalsIgnoreCase() method
 
// Importing required classes
import java.lang.*;
 
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing strings to compare
        String str1 = "GeeKS FOr gEEks";
        String str2 = "geeKs foR gEEKs";
        String str3 = "ksgee orF geeks";
 
        // Comparing strings
        // If we ignore the cases
        boolean result1 = str2.equalsIgnoreCase(str1);
 
        // Both the strings are equal so display true
        System.out.println("str2 is equal to str1 = "
                           + result1);
 
        // Even if we ignore the cases
        boolean result2 = str2.equalsIgnoreCase(str3);
 
        // Both the strings are not equal so display false
        System.out.println("str2 is equal to str3 = "
                           + result2);
    }
}


Output

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

Most Popular

Recent Comments