Java string compareToIgnoreCase and compareTo methods

compareToIgnoreCase and compareTo methods :

Java String methods compareToIgnoreCase and compareTo can be used to compare two strings. In this tutorial, we will learn how these methods work with examples.

compareToIgnoreCase :

The syntax of compareToIgnoreCase method is as below :

public int compareToIgnoreCase(String str)

This is a public method in the String class.

  • It compares two strings lexicographically or dictionary ordering.

  • It takes one string as an argument. This string is compared with the caller string.

  • It returns an integer value, the result of comparison between the caller and the argument string.

  • The output will be 0 if both strings are equal.

  • The output will be a positive integer if the argument string is less than the caller string.

  • The output will be a negative integer if the argument string is greater than the caller string.

  • It doesn’t consider the case of the characters. The case difference is eliminate by calling the method Character.toLowerCase(Character.toUpperCase(character)) on each character of both strings.

  • This method doesn’t provide a_ local sensitive comparison_. For certain locals, the comparison may result in an unsatisfactory ordering.

compareTo :

The syntax of compareTo method is as below :

public int compareTo(String anotherString)

This is a public method in the String class.

  • Similar to the above method, it compares two strings lexicographically.

  • The argument is a String. This argument string is compared with the caller string.

  • It returns one integer value based on the comparison.

  • If both strings are equal, it will return 0

  • It will return one negative number if the caller string is lexicographically less than the argument string.

  • It will return one positive number if the caller string is lexicographically greater than the argument string.

  • The comparison is case-sensitive, unlike compareToIgnoreCase method.

Example :

Let’s try to compare both with a simple example :

class Example{
    public static void main(String[] args){
        String str1 = "Hello";
        String str2 = "Hello";
        String str3 = "hello";
        String str4 = "mello";

        System.out.println(str1.compareToIgnoreCase(str2)+" "+str1.compareTo(str2));
        System.out.println(str1.compareToIgnoreCase(str3)+" "+str1.compareTo(str3));
        System.out.println(str1.compareToIgnoreCase(str4)+" "+str1.compareTo(str4));
        System.out.println(str3.compareToIgnoreCase(str4)+" "+str3.compareTo(str4));
    }
}

Output :

0 0
0 -32
-5 -37
-5 -5

You can see that only the first and the last print statement provides the same output to both methods. For the second and the third statement, the outputs are different. java comparetoignorecase compareto You can also download/edit this program on Github here.

Conclusion :

compareTo and compareToIgnoreCase methods come in handy when we need to compare two strings lexicographically. Try to run the example shown above and drop one comment below if you have any queries.

Similar tutorials :