Java String compareToIgnoreCase() Method



The Java String compareToIgnoreCase() method, is used to compare two strings lexicographically, ignoring lower-case and upper-case differences. Lexicographic comparison means the comparison can be done in an alphabetical order (a-z).

The compareToIgnoreCase() method accepts a parameter as a string that holds the value of the string argument. It returns an integer value, if the strings are lexicographically equal, it returns zero, and if the current string is lexicographically greater than the string argument, it returns positive value; negative value otherwise.

Note − The compareToIgnoreCase() method is not a case-sensitive method, because it ignores the character cases.

Syntax

Following is the syntax of the Java String compareToIgnoreCase() method −

public int compareToIgnoreCase(String str)

Parameters

  • str − This is the String to be compared.

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

If the values of the current string and given string are the same, the compareToIgnoreCase() method returns zero (0).

In the following program, we are instantiating the string class with the value, “Hello” and “Hello”. Using the compareToIgnoreCase() method, we are trying to compare whether the strings are lexicographically equal or not. Since the strings are lexicographically equal, it returns 0.

import java.lang.*;
public class Compare {
   public static void main(String[] args) {
      
      //Instantiate the string class
      String str1 = new String("Hello");
      String str2 = new String("Hello");
      System.out.println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      System.out.println("The given strings are lexicographically equal. " + compare);
   }
}

Output

On executing the above program, it will produce the following result −

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

Example

If the given string value is different from the string argument, this method returns a negative value.

In the following example, we are creating an object of the string class with the value “Java” and “Programming”. Using the compareToIgnoreCase() method, we are trying to compare the string lexicographically.

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

Output

Following is the output of the above program −

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

Example

If the given string values are same but the cases are different, the compareToIgnoreCase() returns zero.

In the following program, we are creating the string literals with the value “Java” and “JAVA”. Using the compareToIgnoreCase() method, we are trying to compare the strings lexicographically. Since the method ignores the cases, it returns the zero.

package com.tutorialspoint.String;
import java.lang.*;
public class Compare {
   public static void main(String[] args) {
      
      //creating an object of the string class
      String str1 = new String("Java");
      String str2 = new String("JAVA");
      System.out.println("The given string values are: " + str1 + " and " + str2);
      
      // using the compareToIgnoreCase() method
      int compare = str1.compareToIgnoreCase(str2);
      System.out.println("The compareToIgnoreCase() method returns: " + compare);
      if(compare == 0) {
         System.out.println("The given strings are lexicographically equal.");
      } else if(compare > 0) {
         System.out.println("The given string is lexicographically greater than the string argument.");
      } else {
         System.out.println("The given string is lexicographically less than the string argument.");
      }
   }
}

Output

After executing the program, it will return the following output −

The given string values are: Java and JAVA
The compareToIgnoreCase() method returns: 0
The given strings are lexicographically equal.
java_lang_string.htm
Advertisements