Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String compareToIgnoreCase() method



compareToIgnoreCase() method is used to compare two strings lexicographically, ignoring case differences. If strings are same, 0 is returned else a negative value is returned.

Syntax

int compareToIgnoreCase(String str)

Parameters

str − string value for comparison.

Return Value

This method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

Example - Comparing two equal strings while ignoring cases

Following is an example of the usage of this method. If the values of the current string and given string are the same, the compareToIgnoreCase() method returns zero (0).

Example.groovy

class Example { 
   static void main(String[] args) { 
      //Instantiate the string class
      String str1 = new String("Hello");
      String str2 = new String("Hello");
      println("The given string values are: " + str1 + " and " + str2);

      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      println("The given strings are lexicographically equal. " + compare);
   } 
}

Output

When we run the above program, we will get the following result −

The given string values are: Hello and Hello
The given strings are lexicographically equal. 0

Example - Comparing two non-equal strings while ignoring cases

Following is an example of the usage of this method. If the values of the current string and given string are not same, the compareToIgnoreCase() method returns a negative value.

Example.groovy

class Example { 
   static void main(String[] args) { 
      //creating an object of the string class
      String str1 = new String("Groovy");
      String str2 = new String("Programming");
      println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      println("The compareToIgnoreCase() method returns: " + compare);
      println("The given strings are not lexicographically equal.");
   } 
}

Output

When we run the above program, we will get the following result −

The given string values are: Java and Programming
The compareToIgnoreCase() method returns: -9
The given strings are not lexicographically equal

Example - Comparing Two Equal Strings while Ignoring Case

Following is an example of the usage of this method. If the given string values are same but the cases are different, the compareToIgnoreCase() returns zero.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // creating an object of the string class
      String str1 = new String("Groovy");
      String str2 = new String("GROOVY");
      println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      println("The compareToIgnoreCase() method returns: " + compare);
      if(compare == 0) {
         println("The given strings are lexicographically equal.");
      } else if(compare > 0) {
         println("The given string is lexicographically greater than the string argument.");
      } else {
         println("The given string is lexicographically less than the string argument.");
      }
   } 
}

Output

When we run the above program, we will get the following result −

The given string values are: Groovy and GROOVY
The compareToIgnoreCase() method returns: 0
The given strings are lexicographically equal.
groovy_strings.htm
Advertisements