Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - String equalsIgnoreCase() method



equalsIgnoreCase() method compares this String to another String, ignoring case considerations.

Syntax

Boolean equalsIgnoreCase(String str)

Parameters

  • str − the String to compare this String against.

Return Value

This method returns true if the argument is not null and the Strings are equal, ignoring case; false otherwise.

Example - Comparing Strings in Case Insensitive Way

Following is an example of the usage of equalsIgnoreCase() method. We are trying to determine whether the given strings are equal or not in case insensitive way.

Example.groovy

class Example {
   static void main(String[] args) {
      String str1 = "sachin tendulkar";
      String str2 = "amrood admin";
      String str3 = "AMROOD ADMIN";
      
      // checking for equality with case ignored
      boolean retval1 = str2.equalsIgnoreCase(str1);
      boolean retval2 = str2.equalsIgnoreCase(str3);
  
      // prints the return value
      println("str2 is equal to str1 = " + retval1);
      println("str2 is equal to str3 = " + retval2);   
   }
}

Output

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

str2 is equal to str1 = false
str2 is equal to str3 = true

Example - Checking String Object with Same String Literal

Following is an example of the usage of equalsIgnoreCase() method. If the given string value is the same as the specified object value, the equalsIgnoreCase() method returns true.

Example.groovy

class Example {
   static void main(String[] args) {
      // instantiate a String class
      String str = new String("Groovy");
      
      // initialize the string object
      String obj = "groovy";
      println("The given string is: " + str);
      println("The object is: " + obj);
      
      // using the equalsIgnoreCase() method
      println("The given string is equal to the specified object or not? " + str.equalsIgnoreCase(obj));
   }
}

Output

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

The given string is: Groovy
The object is: groovy
The given string is equal to the specified object or not? true

Example - Checking String Object with Different String Literal

Following is an example of the usage of equalsIgnoreCase() method. If the given string value is the different from the specified object value, the equalsIgnoreCase() method returns false.

Example.groovy

class Example {
   static void main(String[] args) {
      //create a String literal
      String str = "Hello World";
      
      //initialize the string object
      String obj = "Hello";
      println("The given string is: " + str);
      println("The object is: " + obj);
      
      //using the equalsIgnoreCase() method
      println("The given string is equal to the specified object or not? " + str.equalsIgnoreCase(obj));
   }
}

Output

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

The given string is: Hello World
The object is: Hello
The given string is equal to the specified object or not? false
groovy_strings.htm
Advertisements