Java String compareToIgnoreCase() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, compareToIgnoreCase() compares two strings like compareTo(), but letter case is ignored.

You pass the other string. It is required. The result is a signed int. A result of 0 means equal ignoring case, which is not the same as equals().

Syntax

Call compareToIgnoreCase on the first string.

java
int r = a.compareToIgnoreCase(b);
CallWhat it does
a.compareToIgnoreCase(b)Compares a with b while ignoring letter case.

Only the sign of the result is reliable. For a boolean ignore-case check, prefer equalsIgnoreCase().

Library declarations
java
public int compareToIgnoreCase(String str);

Parameters

ParameterDescription
strRequired. The string to compare with this instance.

Return Value

Return ValueDescription
Less than 0This string comes first when case is ignored.
0The strings match when case is ignored.
Greater than 0This string comes after when case is ignored.

Java Examples

Example 1 — Basic Case-Insensitive Compare

See zero when only case differs, and signed results when letters differ.

java
public class CompareToIgnoreCaseBasic {
    public static void main(String[] args) {
        String a = "Hello";
        String b = "HELLO";
        String c = "World";

        System.out.println(a.compareToIgnoreCase(b));
        System.out.println(a.compareToIgnoreCase(c));
        System.out.println(c.compareToIgnoreCase(a));
    }
}

How It Works

  1. Hello vs HELLO. Same letters ignoring case → 0.
  2. Hello vs World. H/h before W/w → negative.
  3. World vs Hello. Opposite order → positive.
  4. Exact ints vary. Trust the sign, not a fixed magnitude.

Example 2 — compareTo vs compareToIgnoreCase

Side-by-side with compareTo and equalsIgnoreCase.

java
public class CompareToIgnoreCaseVs {
    public static void main(String[] args) {
        String x = "Java";
        String y = "java";

        System.out.println(x.compareTo(y));
        System.out.println(x.compareToIgnoreCase(y));
        System.out.println(x.equalsIgnoreCase(y));
    }
}

How It Works

  1. compareTo. Sees different Unicode values for J and j.
  2. compareToIgnoreCase. Folds case and reports equal → 0.
  3. equalsIgnoreCase. Boolean form of that same idea → true.
  4. Pick by intent. Order → compare APIs; yes/no → equals APIs.

Example 3 — Case-Insensitive Sort

Sort names alphabetically without capitalization scrambling the order.

java
import java.util.Arrays;

public class CompareToIgnoreCaseSort {
    public static void main(String[] args) {
        String[] names = { "bob", "Alice", "charlie", "David" };

        Arrays.sort(names, String::compareToIgnoreCase);

        for (String name : names) {
            System.out.println(name);
        }
    }
}

How It Works

  1. Custom comparator. Arrays.sort uses your compare method.
  2. Method reference. String::compareToIgnoreCase supplies case-blind order.
  3. Alphabetical names. Capitalization no longer jumps Alice ahead of everything.
  4. Alternative. String.CASE_INSENSITIVE_ORDER does the same job.

Frequently Asked Questions

compareToIgnoreCase() compares two strings while ignoring letter case and returns a signed int.
str is required.
Less than 0, 0, or greater than 0. Only the sign is reliable.
compareTo() is case-sensitive. compareToIgnoreCase() ignores case.
For yes/no ignore-case equality, prefer equalsIgnoreCase().

Next: concat()

Join two strings into a new one without changing the originals.

concat() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful