Java StringBuilder compareTo() method



The Java StringBuilder compareTo() method is used to compare two strings lexicographically. It returns an integer value, and the comparison is based on the Unicode value of each character in the StringBuilder object. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the string passed as an argument.

  • The result is a negative integer if this String object lexicographically precedes the argument string.

  • The result is a positive integer if this String object lexicographically follows the argument string.

  • The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would return true.

The compareTo() method accepts a parameter as a string, and it compares the strings lexicographically, the lexicographic compression means in the alphabetical order (a-z). It does not throw any exceptions.

Syntax

Following is the syntax of the Java StringBuilder compareTo() method −

public int compareTo(StringBuilder another)

Parameters

  • another − This is the string to be compare.

Return Value

  • This method returns the value 0, if the argument string is equal to this string.

  • This method returns negative value, if this string is lexicographically less than the string argument.

  • This method returns positive value, if this string is lexicographically greater than the string argument.

Example

If the given string values are equal, the compareTo() method returns 0.

In the following example, we are creating two StringBuilder objects with the value TutorialsPoint. Using the compareTo() method, we are trying the check whether the given strings are lexicographically equal or not. Since the strings are lexicographically equal, this method returns 0.

package com.tutorialspoint.StringBuilder;
public class Compare {
   public static void main(String[] args) {
      
      //instantiating the StringBuilder
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      StringBuilder another = new StringBuilder("TutorialsPoint");
      System.out.println("The given strings are: " + sb + " and " + another);
      
      //using the compareTo() method
      int compare = sb.compareTo(another);
      System.out.println("The compareTo() mmethod returns: " + compare);
      System.out.println("The string " + sb + " and the string " + another + " is lexicographically euqal or not? " + compare);
   }
}

Output

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

The given strings are: TutorialsPoint and TutorialsPoint
The compareTo() mmethod returns: 0
The string TutorialsPoint and the string TutorialsPoint is lexicographically euqal or not? 0

Example

If the current StringBuilder is lexicographically greater than the StringBuilder passed as an argument, this method returns a positive value.

In the following example, we are creating an object of the StringBuilder with the value Hello and World. Using the compareTo() method, we are trying to check whether the StringBuilder lexicographically is greater than the StringBuilder argument or not. Since the StringBuilder is lexicographically greater than the StringBuilder argument, it returns a positive value.

package com.tutorialspoint.StringBuilder;
public class Compare {
   public static void main(String[] args) {
      
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("hello");
      StringBuilder another = new StringBuilder("World");
      System.out.println("The given strings are: " + sb + " and " + another);
      
      //using the compareTo() method
      int compare = sb.compareTo(another);
      System.out.println("The compareTo() method returns: " + compare);
      System.out.println("The StringBuilder " + sb + " is lexicographically euqal to the StringBuilder argument " + another + " or not? " + compare);
   }
}

Output

Following is the output of the above program −

The given strings are: hello and World
The compareTo() method returns: 17
The StringBuilder hello is lexicographically euqal to the StringBuilder argument World or not? 17

Example

If the StringBuilder is lexicographically less than the StringBuilder argument, this method returns a negative value.

In the following example, we are instantiating two the StringBuilder with the value java and language. Using the compareTo() method, we are trying to check whether the StringBuilder lexicographically is greater than the StringBuilder argument or not. Since the StringBuilder is lexicographically less than the StringBuilder argument, it returns a negative value.

package com.tutorialspoint.StringBuilder;
public class Compare {
   public static void main(String[] args) {
      
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("java");
      StringBuilder another = new StringBuilder("language");
      System.out.println("The given strings are: " + sb + " and " + another);
      
      //using the compareTo() method
      int compare = sb.compareTo(another);
      System.out.println("The compareTo() method returns: " + compare);
      if(compare == 0) {
         System.out.println("The StringBuilder is lexicographically euqal to the StringBuilder argument");
      } else if(compare > 0) {
         System.out.println("The StringBuilder is lexicographically greater than the StringBuilder argument");
      } else {
         System.out.println("The StringBuilder is lexicographically less than the StringBuilder argument");
      }
   }
}

Output

The above program, produces the following results −

The given strings are: java and language
The compareTo() method returns: -2
The StringBuilder is lexicographically less than the StringBuilder argument
Advertisements