![]() |
VOOZH | about |
In Java, the String compareToIgnoreCase() method compares two Strings lexicographically means in dictionary order by ignoring the case differences. It evaluates the Unicode values of characters sequentially.
Example 1: Here, we will use the compareToIgnoreCase() method to compare strings that are lexicographically smaller, greater, or the same.
-6 6 0
Explanation: In the above example, "Java" comes before "Python" lexicographically, so the result is negative. "Python" comes after "Java", so the result is positive. "Java" is equal to "java" when ignoring case, so the result is zero.
int compareToIgnoreCase(String s1)
Parameter: s1: The string to compare with the current string by ignoring the case differences.
Return Value: Based on the lexicographical order, this method returns an integer value
Example 2: Here, we will use the compareToIgnoreCase()to check if two strings are equal, ignoring case differences.
The strings are equal (case-insensitive).
Explanation: In the above example, the method returns 0 if the strings are equal when ignoring case.
Example 3: Here, the compareToIgnoreCase() method throws a NullPointerExceptionif the string which is compared is null.
Cannot compare to a null string.