Thursday, July 4, 2024
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
Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments