Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String compareToIgnoreCase() method
Syntax and Examples



Introduction

The compareToIgnoreCase() method in Java is a handy tool for comparing two strings without worrying about the case (uppercase or lowercase) of the characters. Imagine you're sorting a list of names – do you want 'Alice' to come before 'bob' just because 'b' is alphabetically earlier than 'a'? Probably not! compareToIgnoreCase() lets you compare strings while ignoring those capitalization differences.

Syntax

public int compareToIgnoreCase(String str)

Parameters

Parameter Description
str The string to be compared with the current string.

Return Value

The compareToIgnoreCase() method returns an integer value based on the comparison:

  • If the first string is lexicographically less than the second string, it returns a negative number.
  • If the first string is lexicographically greater than the second string, it returns a positive number.
  • If the strings are equal (ignoring case), it returns 0.

Examples

Example 1: Basic Comparison

This example demonstrates how to compare two simple strings, 'Java' and 'java', using compareToIgnoreCase().

public class Example1 {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "java";

        int result = str1.compareToIgnoreCase(str2);

        System.out.println("Result: " + result); // Output the comparison result
    }
}
Result: 0

Explanation: Because we're using compareToIgnoreCase(), Java ignores the case difference. Both strings are considered equal.

Example 2: Case-Sensitive vs. Case-Insensitive

This example shows the difference between compareTo() (case-sensitive) and compareToIgnoreCase() (case-insensitive).

public class Example2 {
    public static void main(String[] args) {
        String str1 = "Java";
        String str2 = "java";

        int caseSensitiveResult = str1.compareTo(str2);
        int caseInsensitiveResult = str1.compareToIgnoreCase(str2);

        System.out.println("Case-sensitive Result: " + caseSensitiveResult); // Output the case-sensitive result
        System.out.println("Case-insensitive Result: " + caseInsensitiveResult); // Output the case-insensitive result
    }
}
Case-sensitive Result: 32
Case-insensitive Result: 0

Explanation: compareTo() considers capitalization, so 'J' comes after 'j', resulting in a positive value. compareToIgnoreCase() ignores case, returning 0.

Example 3: Comparing with Different Strings

This example compares the string "Apple" with "banana", demonstrating how to handle strings that are not equal (ignoring case).

public class Example3 {
    public static void main(String[] args) {
        String str1 = "Apple";
        String str2 = "banana";

        int result = str1.compareToIgnoreCase(str2);

        System.out.println("Result: " + result); // Output the comparison result
    }
}
Result: -45

Explanation: 'A' comes before 'b', so 'Apple' is lexicographically less than 'banana'. compareToIgnoreCase() returns a negative number, indicating that the first string is smaller.

Example 4: Comparing Empty String

This example demonstrates comparison with an empty string. An empty string is considered to be smaller than any non-empty string.

public class Example4 {
    public static void main(String[] args) {
        String str1 = "";
        String str2 = "Hello";

        int result = str1.compareToIgnoreCase(str2);

        System.out.println("Result: " + result); // Output the comparison result
    }
}
Result: -28

Explanation: An empty string is considered smaller than any other non-empty string. The returned negative value signifies this.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M