compareTo() is a method in Java that compares the string given with the current string in a lexicographical manner. Comparison is done on the basis of the Unicode value of characters available in the string.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Following are the different conditions in the compareTo() method.
Syntax:
Below is the syntax of compareTo() method:
public int compareTo(String s2)
Here, s2 is the string that is used for comparison with the current string. An integer value will be returned on calling this method.
compareTo() method can be used in three ways.
Given below are the examples of compareTo Java:
Java program to implement compareTo method that compares two strings.
Code:
public class compareToExample { //main method public static void main(String args[]) { //create three strings for comparison String s1 = "Happiness lies within you"; String s2 = "Happiness LIES WITHIN YOU"; String s3 = "Happiness lies within you"; //store comparison result of s1 and s2 in v1 int V1 = s1.compareTo( s2 ); System.out.println( " Compare s1 and s2 : "+ V1 ) ; //store comparison result of s1 and s3 in v2 int v2 = s1.compareTo( s3 ); System.out.println( " Compare s1 and s3 : "+ v2 ) ; //store comparison result of s2 and s3 in v3 int v3 = s2.compareTo(s3); System.out.println(" Compare s2 and s3 : "+ v3 ) ; }}
Output:
In this program, three strings are created s1, s2 and s3. Three variables, v1, v2 and v3, are also created for storing the comparison results of s1&s2, s1&s3, and s2&s3, respectively. It can be seen that a positive number is returned on comparing s1 & s2, and a negative number is returned on comparing s2 &s3. As both s1 and s3 are equal, 0 is returned in the second case.
Java program to implement compareTo method that compares a string and an object.
Code:
public class compareToExample { //main method public static void main(String args[]) { //create a string for comparison String s1 = "Happiness lies within you"; //store comparison result of s1 and ARGUMENT in v2 int v2 = s1.compareTo( "Happiness LIES within you"); System.out.println( " Compare s1 and argument : "+ v2 ) ; } }
Output:
In this program, a string s1 and variable v1 are created first. Another string is passed as an argument in the compareTo() method, and it can be seen that a positive number is returned on comparing s1 and argument.
Java program to find the length of a string using the compareTo method.
Code:
public class compareToExample { //main method public static void main(String args[]) { //create three strings for comparison String s1 = "Happiness lies within you"; String s2 = ""; //length will be returned in positive int V1 = s1.compareTo( s2 ); System.out.println( " Length of s1 : "+ V1 ) ; //length will be returned in negative int v2 = s2.compareTo( s1 ); System.out.println( " Length of s1 : "+ v2 ) ; } }
Output:
In this program, two strings are created, s1 and s2, where s2 is a null string. If the given string is compared with a nullstring, then the length of the non-empty string will be returned. If a comparison is done in reverse order, a negative value of the length will be returned.
Java program to implement compareToIgnoreCase method that compares two strings.
Code:
public class compareToExample { //main method public static void main(String args[]) { //create three strings for comparison String s1 = "Happiness lies within you"; String s2 = "Happiness LIES WITHIN YOU"; String s3 = "Happiness lies within you"; //store comparison result of s1 and s2 in v1 int V1 = s1.compareToIgnoreCase( s2 ); System.out.println( " Compare s1 and s2 : "+ V1 ) ; //store comparison result of s1 and s3 in v2 int v2 = s1.compareTo( s3 ); System.out.println( " Compare s1 and s3 : "+ v2 ) ; //store comparison result of s2 and s3 in v3 int v3 = s2.compareToIgnoreCase(s3); System.out.println(" Compare s2 and s3 : "+ v3 ) ; } }
Output:
As already seen, compareToIgnoreCase ignores the case and compares the strings. As the three strings differ only in cases, 0 will be returned on calling this method.
Java program to implement compareToIgnoreCase method that compares a string and an object.
Code:
public class compareToExample { //main method public static void main(String args[]) { //create a string for comparison String s1 = "Happiness lies within you"; //store comparison result of s1 and ARGUMENT in v2 int v2 = s1.compareToIgnoreCase( "Happiness LIES within you"); System.out.println( " Compare s1 and argument : "+ v2 ) ; } }
Output:
In this program, a string s1 and variable v1 are created first. Another string is passed as an argument in the compareToIgnoreCase() method, and it can be seen that 0 is returned as the case is ignored.
compareTo() is a Java method that compares the string given with the current string in a lexicographical manner. In this article, different aspects such as syntax, working, and examples of the compareTo() method is seen in detail.
The above is the detailed content of compareTo Java. For more information, please follow other related articles on the PHP Chinese website!