Java Program example to demonstrate String compareToIgnoreCase method

Program

public class StringCompareToIgnoreCase
{
        public static void main(String[] args)
        {
                String str1 = "Welcome to oodlescoop";
                String str2 = "Welcome to oodlescoop";
                String str3 = "Hello all";
                String str4 = "Welcome to oodlescoop tutorials";
                String str5 = "WELCOME TO OODLESCOOP";
                System.out.println(str1.compareToIgnoreCase(str2) + "\t: strings are equal");
                System.out.println(str1.compareToIgnoreCase(str3) + "\t: first string is greater");
                System.out.println(str1.compareToIgnoreCase(str4) + "\t: second string is greater");
                System.out.println(str1.compareToIgnoreCase(str5) + "\t: equal inspite of case of the text");
        }
}

Purpose

This Java program demonstrates how to compare strings lexicographically while ignoring case differences using the compareToIgnoreCase method of the String class.

1. Class Definition

public class StringCompareToIgnoreCase
  • Defines the class StringCompareToIgnoreCase.

2. Main Method

public static void main(String[] args)
  • Entry point for the program.

3. Define Strings

String str1 = "Welcome to oodlescoop";
String str2 = "Welcome to oodlescoop";
String str3 = "Hello all";
String str4 = "Welcome to oodlescoop tutorials";
String str5 = "WELCOME TO OODLESCOOP";
  • str1 and str2: Identical strings.
  • str3: A string that is lexicographically smaller than str1.
  • str4: A string that is lexicographically larger than str1.
  • str5: Same as str1 but with all uppercase letters.

4. Compare Strings Using compareToIgnoreCase

System.out.println(str1.compareToIgnoreCase(str2) + "\t: strings are equal");
System.out.println(str1.compareToIgnoreCase(str3) + "\t: first string is greater");
System.out.println(str1.compareToIgnoreCase(str4) + "\t: second string is greater");
System.out.println(str1.compareToIgnoreCase(str5) + "\t: equal inspite of case of the text");
  • compareToIgnoreCase Method:
    • Compares two strings lexicographically, ignoring case differences.
    • Returns:
      • 0: If the two strings are equal when case is ignored.
      • A positive number: If the first string is lexicographically greater.
      • A negative number: If the first string is lexicographically smaller.

Detailed Explanation of Comparisons

1. Compare str1 and str2

str1.compareToIgnoreCase(str2)
  • Both strings are identical, and case is the same.
  • Result: 0 (strings are equal).
  • Output:
    0	: strings are equal
    

2. Compare str1 and str3

str1.compareToIgnoreCase(str3)
  • str1: "Welcome to oodlescoop".
  • str3: "Hello all".
  • Lexicographically, "Welcome to oodlescoop" is greater because 'W' comes after 'H' in Unicode order, ignoring case.
  • Result: A positive number.
  • Output:
    15	: first string is greater
    

3. Compare str1 and str4

str1.compareToIgnoreCase(str4)
  • str1: "Welcome to oodlescoop".
  • str4: "Welcome to oodlescoop tutorials".
  • Lexicographically, str1 is smaller because str4 has additional characters.
  • Result: A negative number.
  • Output:
    -9	: second string is greater
    

4. Compare str1 and str5

str1.compareToIgnoreCase(str5)
  • str1: "Welcome to oodlescoop".
  • str5: "WELCOME TO OODLESCOOP".
  • Ignoring case, both strings are identical.
  • Result: 0 (strings are equal).
  • Output:
    0	: equal inspite of case of the text
    

Key Points About compareToIgnoreCase

  1. Case-Insensitive Comparison:

    • It ignores the case of the characters when comparing.
    • For example:
      • 'a' is considered equal to 'A'.
  2. Lexicographical Order:

    • Even though case is ignored, the method still compares characters lexicographically.
  3. String Length:

    • If two strings are identical up to the length of the shorter string, the longer string is considered greater.

Output

0	: strings are equal
15	: first string is greater
-10	: second string is greater
0	: equal inspite of case of the text