HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java String compareToIgnoreCase() method example

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?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces