The equalsIgnoreCase()
method is a built-in method in the Java programming language’s String
class. It is used to compare two strings while ignoring their cases. In other words, it checks whether two strings are equal regardless of the case of the characters in the strings.
The method takes one parameter, which is the string to be compared with the current string. The method returns a boolean value indicating whether the two strings are equal or not, ignoring case considerations. The method signature is as follows:
public boolean equalsIgnoreCase(String anotherString)
Here’s an example usage of the equalsIgnoreCase()
method:
String str1 = "HELLO"; String str2 = "hello"; if (str1.equalsIgnoreCase(str2)) { System.out.println("The two strings are equal (ignoring case)"); } else { System.out.println("The two strings are not equal (ignoring case)"); }
In the above example, the equalsIgnoreCase()
method is used to compare str1
and str2
to check if they are equal or not, ignoring case considerations. Since the two strings have the same content, but different cases, the method returns true
, and the output is “The two strings are equal (ignoring case)”.
Internal implementation:
The implementation of the equalsIgnoreCase()
method in Java is quite simple. Here’s an overview of how it works:
- The method first checks if the argument passed to it is null. If it is null, then it returns false, indicating that the two strings are not equal.
- The method then checks if the lengths of the two strings are equal. If they are not equal, then it returns false, indicating that the two strings are not equal.
- If the lengths of the two strings are equal, then the method iterates through each character of the strings, comparing them one by one. If any character is found to be different (ignoring case considerations), then it returns false, indicating that the two strings are not equal.
- If the method has iterated through all characters of the strings and found no differences, then it returns true, indicating that the two strings are equal (ignoring case considerations).
Here’s the source code for the equalsIgnoreCase()
method in Java:
public boolean equalsIgnoreCase(String anotherString) { return (this == anotherString) ? true : (anotherString != null) && (anotherString.length() == length()) && regionMatches(true, 0, anotherString, 0, length()); }
As you can see, the implementation checks for null and then calls the regionMatches()
method with the true
flag to ignore the case while comparing the strings. The regionMatches()
method is another built-in method in the String
class that performs a case-insensitive comparison of two string regions.
Java String equalsIgnoreCase() Method Example:
Sure! Here’s an example that demonstrates how to use the equalsIgnoreCase()
method in Java:
public class Main { public static void main(String[] args) { String str1 = "hello"; String str2 = "HELLO"; if (str1.equalsIgnoreCase(str2)) { System.out.println("The two strings are equal (ignoring case)"); } else { System.out.println("The two strings are not equal (ignoring case)"); } } }
In the above example, we define two strings str1
and str2
. We then use the equalsIgnoreCase()
method to compare these two strings, ignoring case considerations. Since the two strings have the same content, but different cases, the method returns true
, and the output is “The two strings are equal (ignoring case)”.
Here’s the output of the above example:
The two strings are equal (ignoring case)
I hope this example helps you understand how to use the equalsIgnoreCase()
method in Java.