Java String Comparison Methods.


Java String class provides different comparison methods namely:

The compareTo() method

This method compares two Strings lexicographically. This method returns  a negative integer if current String object lexicographically precedes the argument string.  a positive integer if current String object lexicographically follows the argument  true when the strings are equal.

Example

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials", str2 = "point";
      // comparing str1 and str2
      int retval = str1.compareTo(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");
      }
   }
}

Output

str1 is less than str2


The compareToIgnoreCase() method

This method is same as compareTo() method, it compares two strings lexicographically, ignoring case differences.

Example

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");
      }
   }
}

Output

str1 is equal to str2

The equals() method

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Example

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "sachin tendulkar";
      String str2 = "amrood admin";
      String str3 = "amrood admin";
     
      // checking for equality
      boolean retval1 = str2.equals(str1);
      boolean retval2 = str2.equals(str3);
     
      // prints the return value
      System.out.println("str2 is equal to str1 = " + retval1);
      System.out.println("str2 is equal to str3 = " + retval2);
   }
}

Output

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


The equalsIgnoreCase() method 

This method is equal to the ignoreCase() method, except Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

Example

import java.lang.*;
public class StringDemo {
   public 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
      System.out.println("str2 is equal to str1 = " + retval1);
      System.out.println("str2 is equal to str3 = " + retval2);
   }
}

Output

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

Updated on: 26-Feb-2020

434 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements