The Wayback Machine - https://web.archive.org/web/20240213201341/https://www.geeksforgeeks.org/java-string-equalsignorecase-method-with-examples/
Open In App
Related Articles

Java String equalsIgnoreCase() Method with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 11 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials