compareToIgnoreCase Java

In Java, the method compareToIgnoreCase() belongs to the String class that belong to java.lang package. It is used for comparing any two strings by ignoring the lower- and upper-case differences. The method does the comparison of strings using the Unicode value of each character present in both of the strings. The way we pass both the strings to the method compareToIgnoreCase() is similar to the method compareTo(), and the following results is returned by the method.

  • A positive number is returned if string 1 is greater than string 2.
  • A negative number is returned if string 1 is less than string 2.
  • Zero is returned if string 1 is equal to the string 2.

Note that in the above-written results, we have assumed that string 1 is invoking the method and string 2 is passed as the parameter.

Syntax:

The syntax of the method is:

Parameter: String s is the parameter of the method that is to be compare.

Return Type: It returns an integer value.

CompareToIgnoreCase Java Program

Let's see how one can use the method in a Java program.

FileName: CompareToIgnoreCase.java

Output:

0
-10
1
10

Explanation: Since we are using the compareToIgnoreCase() for string comparison; therefore, string1 and string2 are treated as the same strings. Therefore, the first output is 0.

For the second output, the string string1 ("Book") is compared with the string3 ("look"), and the gap between 'B' and 'l' is 10 as per the alphabetical sequence (remember the case sensitiveness is ignored. Thus, 'l' can be treated as 'L'). Since 'B' comes after 'l'; therefore, -10 is the answer.

For the third output, the character 'B' is compared with the character 'a', and we know that the gap between 'B' and 'a' is 1. So, the output is 1.

For the fourth output, "Book" is compared with "BEEN". Here, the first character of "Book" is compared with the first character of "Been", and we get 0. So, the second character of both the strings ('o' of "Book", and 'E' of BEEN) is taken into consideration, and we see that the gap between 'o' and 'E' is 10. Since 'o' comes after 'E'; therefore, the output is plus 10.

Custom compareToIgnoreCase() Method

We can also define own compareToIgnoreCase() method in Java. The method takes two parameters: one is string string1, and the other is string string2 and will return an integer. The following program illustrates the same.

FileName: CompareToIgnoreCase1.java

Output:

0
-10
1
10

Points to Remember

The following are some important points to remember while working with the method compareToIgnoreCase().

1. For maintaining the case insensitiveness, all the alphabets of both the strings are converted into lowercase letters (not in uppercase). The following example will make things clearer.

FileName: CompareToIgnoreCase2.java

Output:

-44

Explanation: The Unicode value of '6' is 54 and of 'B' is 66. So, the answer should be 54 - 66 = -12. However, we are getting -44. It is because the string "Book" is getting converted into "book", and the Unicode value of 'b' is 98. Thus, 54 - 98 is -44, which is shown in the output. It shows that the alphabets of the strings are converted into lowercase letters, not into uppercase letters. It is because of this reason our custom compareToIgnoreCase() method uses the toLoweCase() method instead of toUpperCase() method.

2. In the method compareToIgnoreCase(), the comparison stops immediately when the first mismatch is found. It is evident when we look at the first program of this section, where "Book" is compared with "abc". Here, the first characters of each string are compared, and we get a mismatch. Thus, the rest of the characters of both the strings are ignored, and the comparison loop stops there to return the appropriate results.

3. When there are some extra characters present in any one of the strings, and the remaining characters match with the other string, then only the count of the extra characters is returned. Observe the following example.

FileName: CompareToIgnoreCase3.java

Output:

3

Explanation: Here, string string1 has some extra letters in the end. The comparison happens till the character 'k'; after that, there is no letter in the string string2. For the substring "sss", it is not possible to make a comparison. Therefore, only the numbers of extra letters are counted and returned. Hence, the output is 3. Note that instead of string string1, if there is some extra letters in the string string2, then the output will in minus. For example: "book".compareToIgnoreCase("Booksss"); results in -3.