Java.lang.String.compareToIgnoreCase() Method


Advertisements


Description

The java.lang.String.compareToIgnoreCase() method compares two strings lexicographically, ignoring case differences.

Declaration

Following is the declaration for java.lang.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.

Exception

NA

Example

The following example shows the usage of java.lang.String.compareToIgnoreCase() method.

package com.tutorialspoint;

import java.lang.*;

public class StringDemo {

   public static void main(String[] args) {

      String str1 = "tutorials", str2 = "TUTORIALS";
   
      // comparing str1 and str2 with case ignored
      int retval = str1.compareToIgnoreCase(str2);

      // prints the return value of the comparison
      if (retval > 0) {
         System.out.println("str1 is greater than str2");
      } else if (retval == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is less than str2");
      }
   }
}

Let us compile and run the above program, this will produce the following result −

str1 is equal to str2

java_lang_string.htm

Advertisements