Java compareToIgnoreCase () method
compareToIgnoreCase () method is used to compare two strings lexicographically, ignoring case.
grammar
int compareToIgnoreCase(String str)
parameter
str - the string to compare.
return value
- If the argument string is equal to this string, it returns a value of 0;
- If this string is less than the string parameter, it returns a value less than zero;
- If this string is greater than the string parameter, a value greater than 0 is returned.
Examples
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 above program execution results:
0 -3 3