Java String compareToIgnoreCase() Method

Java String compareToIgnoreCase() Method

When comparing strings in Java, the compareToIgnoreCase() method is useful if you want to ignore case differences. Unlike compareTo(), which considers uppercase and lowercase letters as different, compareToIgnoreCase() treats them as the same.

In this blog, we will learn how to use the compareToIgnoreCase() method to compare strings in Java with examples. We will also see how it helps in comparing strings without worrying about uppercase and lowercase letters.

Table of Contents:

What is the compareToIgnoreCase() Method in Java?

The compareToIgnoreCase() method in Java is used to compare two strings lexicographically, ignoring case differences. It works similarly to compareTo(), but it does not consider cases while comparing characters. 

Syntax:

int result = string1.compareToIgnoreCase(string2);

Parameters:

  • string1 is the first string to compare
  • string2 is the second string to compare
  • result is an integer either( 0, positive or negative).

Return Type: It returns an integer value 

  • Returns 0: If both strings are equal.
  • Negative value: If string1 is lexicographically less than string2.
  • Positive value: If string1 is lexicographically greater than string2.

Examples of compareToIgnoreCase() Method in Java

Here are the following examples of the compareToIgnoreCase() Method in Java that helps you to understand this method in more detail:

Example 1: Comparing Strings with Different Cases

In this example, we will compare three strings of different cases using the compareToIgnoreCase() Method in Java:

Example:

Java

Output:

Explanation: This program compares three strings using the compareToIgnoreCase() method, 

which ignores case differences. 

  • Since “apple” and “Apple” are the same when the case is ignored, the result is 0. 
  • The comparison between “apple” and “Banana” is based on dictionary order, so it returns a negative value.
  •  while “Banana” compared to “apple” returns a positive value.

Example 2: compareToIgnoreCase() with null checking to avoid NullPointerException

Here’s an example of using compareToIgnoreCase() with null checking to avoid NullPointerException:

Example:

Java

Output:

Explanation: If either str1 or str2 is null, the program prints a message instead of calling compareToIgnoreCase(), preventing a NullPointerException.

Conclusion

In this blog, we have explored how we can compare two strings using the compareToIgnoreCase() method in Java. We have seen how this method compares strings while ignoring case differences, making it useful for case-insensitive comparisons. We also discussed how to handle null values to avoid exceptions during comparison.

If you want to learn more about Java, you may refer to our Java Course.

Some Other Methods to Compare Strings in Java

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner