Java compareToIgnoreCase() Method
The compareToIgnoreCase() method is used to compare two strings lexicographically, ignoring case differences.
Syntax
int compareToIgnoreCase(String str)
Parameters
-
str -- The string to be compared.
Return Value
- If the argument string is equal to this string, the return value is 0;
- If this string is lexicographically less than the argument string, a value less than 0 is returned;
- If this string is lexicographically greater than the argument string, a value greater than 0 is returned.
Example
public class Test {
public static void main(String args[]) {
String str1 = "STRINGS";
String str2 = "Strings";
String str3 = "Strings123";
int result = str1.compareToIgnoreCase( str2 );
System.out.println(result);
result = str2.compareToIgnoreCase( str3 );
System.out.println(result);
result = str3.compareToIgnoreCase( str1 );
System.out.println(result);
}
}
The execution result of the above program is:
0
-3
3
Java String class