Java Tutorial Java Advanced Java References

Java String - compareToIgnoreCaseIgnoreCase() Method



The Java compareToIgnoreCase() method is used to compare two strings lexicographically, ignoring case differences.

Syntax

public int compareToIgnoreCase(String str)

Parameters

str specify the string to be compared.

Return Value

Returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

Exception

NA.

Example:

In the example below, compareToIgnoreCase() method is used to compare strings lexicographically, ignoring case differences.

public class MyClass {
  public static void main(String[] args) {
    String str1 = "Hello World";
    String str2 = "HELLO WORLD";
    String str3 = "Learn Java.";

    //comparing str1 and str2
    System.out.println(str1.compareToIgnoreCase(str2));

    //comparing str1 and str3
    System.out.println(str1.compareToIgnoreCase(str3));
  }
}

The output of the above code will be:

0
-4

❮ Java String Methods