Java String compareToIgnoreCase() method example

Java String compareToIgnoreCase() method compares two strings lexicographically ignoring case. This method is same as String.compareTo() method except compareTo() method is case sensitive.

1. String compareToIgnoreCase() method

In compareToIgnoreCase() method, two strings are compared ignoring case lexicographically (dictionary order). The first string is the String object itself on which method is called. Second string is argument to method.

This method does the string comparison based on the Unicode value of each character in the strings.

1.1. Method return type

The result of this method is in integer value where –

  1. positive integer – means string object lexicographically follows the argument string.
  2. negative integer – means string object lexicographically precedes the argument string.
  3. zero – means both strings are equal.

1.2. Method implementation

This method uses CaseInsensitiveComparator class which is static inner class of String class. String comparison is done in it’s compare() method.

public int compare(String s1, String s2) {
    int n1 = s1.length();
    int n2 = s2.length();
    int min = Math.min(n1, n2);
    for (int i = 0; i < min; i++) {
        char c1 = s1.charAt(i);
        char c2 = s2.charAt(i);
        if (c1 != c2) {
            c1 = Character.toUpperCase(c1);
            c2 = Character.toUpperCase(c2);
            if (c1 != c2) {
                c1 = Character.toLowerCase(c1);
                c2 = Character.toLowerCase(c2);
                if (c1 != c2) {
                    // No overflow because of numeric promotion
                    return c1 - c2;
                }
            }
        }
    }
    return n1 - n2;
}

2. Java String compareToIgnoreCase() example

Java program to compare two strings in case-insensitive manner. Notice that compareTo() and compareToIgnoreCase() methods behave in same way, except later is case-insensitive.

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( "apple".compareTo("BANANA") );                     //31
        System.out.println( "apple".compareToIgnoreCase("banana") );            //-1
        
        System.out.println( "cherry".compareTo("cherry") );                     //0
        System.out.println( "cherry".compareToIgnoreCase("CHERRY") );           //0
    }
}

3. compareToIgnoreCase() vs equalsIgnoreCase()

Learn the main differences between compareToIgnoreCase() vs equalsIgnoreCase() methods.

  • compareToIgnoreCase() compare lexicographically (dictionary ordering).
    equalsIgnoreCase() checks for string equality that both strings are equal or not. Though both are case-insensitive.
  • Return type of compareToIgnoreCase() is integer type which represents a string is greater than, less than or equals to another string.
    Return type of equalsIgnoreCase() is boolean which means both strings are equals or not.

4. Java String compareTo() example

Java program to compare strings using String compareTo() method.

public class Main 
{
    public static void main(String[] args) 
    {
        System.out.println( "apple".compareTo("banana") );  //-1 - apple comes before banana
        System.out.println( "apple".compareTo("cherry") );  //-2 - apple comes before cherry
        System.out.println( "cherry".compareTo("banana") ); //1  - cherry comes after banana
        System.out.println( "cherry".compareTo("cherry") ); //0  - Both strings are equal
    }
}

Happy Learning !!

Reference: String Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.