Java compareToIgnoreCase() Method

Java String类Java String Class


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 less than the string argument, a value less than 0 is returned;
  • If this string is greater than the string argument, 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 output of the above program is:

0
-3
3

Java String类Java String Class

Other Extensions