String compareToIgnoreCase() Java

String compareToIgnoreCase() Java | Java provides the compareToIgnoreCase() built-in method in the String class to compare two strings lexicographically i.e. in dictionary order by ignoring the case.

There might be a situation where we need to compare two strings ignoring their cases. In such a situation, we can use the compareToIgnoreCase() method of the Java String class. The compareToIgonereCase() method return type is an integer. The compareToIgnoreCase() method returns 0 if the strings are equal or else it returns some other integer if the strings are not equal.

If this string is greater than the argument string then the method returns a positive number. If this string is lesser than the argument string then the method returns a negative number. Else if this string is equal to the argument string then it returns zero.

The method details of the compareToIgnoreCase method are as follows:- int compareToIgnoreCase(String str)
Parameters:- string which needs to be compared.
Return type:- integer.

Return Valuestr1.compareToIgnoreCase(str2)
0str1 is equal to str2
<0str1 is less than str2
>0str1 is greater than str2

Java String class also contains compareTo() and equals() methods to compare the strings. But there are lots of differences between these three methods. See the below example to understand the usage of the compareToIgnoreCase() method.

Example-1:-
String1 = “Hello”
String2 = “HELLO”
Result => 0

Example-2:-
String1 = “HeLLo”
String2 = “hellO”
Result => 0

Example-3:-
String1 = “Hello”
string2 = “Hi”
Result => 2

String compareToIgnoreCase() Java Example

public class Main {

   public static void main(String args[]) {
      String string1 = "Strings once declared cannot be changed";
      String string2 = "Strings once declared CANNOT BE CHANGED";
      String string3 = "Integers are one of the datatype used in Java";

      int res = string1.compareToIgnoreCase(string2);
      System.out.println(res);

      res = string2.compareToIgnoreCase(string3);
      System.out.println(res);

      res = string3.compareToIgnoreCase(string1);
      System.out.println(res);
   }
}

Output:-

0
10
-10

The string1 and string2 contain the same data but some words are in different cases. Whereas string3 is completely different from string1 and string2. When we call string2.compareToIgnoreCase(string3) then it returns 10 and when we reverse this and the argument string then we get the result as -10.

public class Main {
   public static void main(String[] args) {
      String string1 = "java";
      String string2 = "JAVA";
      String string3 = "JAva";
      String string4 = "Jaaava";
      String string5 = "JAAAVA";
      
      System.out.println(string1.compareToIgnoreCase(string2));
      System.out.println(string1.compareToIgnoreCase(string3));
      System.out.println(string1.compareToIgnoreCase(string4));
      System.out.println(string1.compareToIgnoreCase(string5));
   }
}

Output:-

0
0
21
21

Java String class contains equals(), equalsIgnoreCase(), compareTo(), and compareToIgnoreCase() methods to compare two string objects, we can use one of them based on our requirement.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *