compareTo(): This method is available in java.lang.StringBuffer class of Java.
Syntax:
int java.lang.StringBuffer.compareTo(StringBuffer another)
This method takes one argument of type StringBuffer as its parameter. This method compares two StringBuffer instances lexicographically.
Parameters: One parameter is required for this method.
another: the StringBuffer to be compared with.
Returns:
1. The value 0 if this StringBuffer contains the same character sequence as that of the argument StringBuffer.
2. A negative integer if this StringBuffer is lexicographically less than the StringBuffer argument.
3. A positive integer if this StringBufferis lexicographically greater than the StringBuffer argument.
For Example:
StringBuffer str = new StringBuffer("Hello World")
StringBuffer another = new StringBuffer("Hello")
str.compareTo(another) = > It returns 6.
Approach
Java
public class CompareTo {public static void main(String[] args) {StringBuffer str = new StringBuffer("Hello World");StringBuffer another = new StringBuffer("Hello");System.out.println(str.compareTo(another));}}
Output:
6
No comments:
Post a Comment