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
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
String str1 = "GeeKS FOr gEEks" ;
String str2 = "geeKs foR gEEKs" ;
String str3 = "ksgee orF geeks" ;
boolean result1 = str2.equalsIgnoreCase(str1);
System.out.println( "str2 is equal to str1 = "
+ result1);
boolean result2 = str2.equalsIgnoreCase(str3);
System.out.println( "str2 is equal to str3 = "
+ result2);
}
}
|
Output
str2 is equal to str1 = true
str2 is equal to str3 = false
Last Updated :
11 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...