Java String equalsIgnoreCase()

The equalsIgnoreCase() Method is used to compare a specified 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.

Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:

  • The two characters are the same (as compared by the == operator)

  • Applying the method Character.toUpperCase(char) to each character produces the same result

  • Applying the method Character.toLowerCase(char) to each character produces the same result

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

str : another string i.e. compared with this string.

Returns

It returns true if characters of both strings are equal ignoring case otherwise false.

Java String equalsIgnoreCase() method example
public class Test1 { public static void main(String args[]) { String s1="cprogramcoding"; String s2="cprogramcoding"; String s3="CPROGRAMCODING"; String s4="java"; System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same } }


Output:
true true false
Java String equalsIgnoreCase() Method Example

See an example where we are testing string equality among the strings.

import java.util.ArrayList; public class Test1 { public static void main(String[] args) { String str1 = "Pavi"; list.add("Rama"); list.add("RAVI"); list.add("Prakesh"); list.add("Leethesh"); list.add("Soohith"); for (String str : list) { if (str.equalsIgnoreCase(str1)) { System.out.println("Pavi is present"); } } } }


Output:
Pavi is present
public class Test1 { public static void main(String[] args) { String columnist1 = "Rama"; String columnist2 = "Soohith"; String columnist3 = "Leethesh"; // Test any of the above Strings equal to one another boolean equals1 = columnist1.equalsIgnoreCase(columnist2); boolean equals2 = columnist1.equalsIgnoreCase(columnist3); System.out.println(); // Display the results of the equality checks. System.out.println("\"" + columnist1 + "\" equals \"" + columnist2 + "\"? " + equals1); System.out.println("\"" + columnist1 + "\" equals \"" + columnist3 + "\"? " + equals2); System.out.println(); } }


Output:
"Rama" equals "Soohith"? false "Rama" equals "Leethesh"? false



Instagram