The String compareTo() method compares the two strings lexicographically. Each character of both the strings is converted into the Unicode value for comparison. If both strings are equal, this method returns 0; otherwise, it returns the positive or negative value.
Java String compareTo
Java String compareTo() is a built-in function used to compare two strings lexicographically where each character of both the strings is converted into a Unicode value.
The compareTo() method returns three types of value, if both the string is equal, then it returns 0; if the first string is lexicographically greater than the second string, then it returns a positive value, if the second string is lexicographically greater than the first one, then it returns a negative value.
Variants of Function
There are three variants of this method; they are:
int compareTo(Object obj):
The compareTo() method compares one string with another string object.
Syntax
int compareTo(Object obj)
Here obj is the string object to be compared.
Here the comparison is between the string and the object.
For example, string1.compareTo(“Just a String object”) where string1 is the literal and its values are compared with the string specified in the method argument.
See the following program.
class CompareToStr { public static void main(String args[]) { // Initializing the string String str = "Appdividend"; // Declaring string object String str1 = new String("Appdividend"); // str1 is the object String str2 = new String("appdividend"); // str2 is the object System.out.print("Difference between Appdividend and str1 is: "); System.out.println(str.compareTo(str1)); System.out.print("Difference between Appdividend and str2 is: "); System.out.println(str.compareTo(str2)); } }
See the output.
int compareTo(String anotherString)
The method works the same as the above method, but the difference is it compares with another string lexicographically.
Syntax
int compareTo(String anotherString)
Here anotherString indicates another string with which the first string is to be compared.
class CompareToStr { public static void main(String args[]) { // Initializing the string String str = "Appdividend"; // Declaring another string String str1 = "Appdividend"; // str1 is the string String str2 = "appdividend"; // str2 is the string System.out.print("Difference between Appdividend and str1 is: "); System.out.println(str.compareTo(str1)); System.out.print("Difference between Appdividend and str2 is: "); System.out.println(str.compareTo(str2)); } }
See the output.
int compareToIgnoreCase(String str)
The method is used to compare two strings but ignores case differences. This means it ignores the uppercase and lowercase of a string.
Syntax
int compareToIgnoreCase(String str)
Here str is the string with which we have to compare.
See the following program.
class CompareToStr { public static void main(String args[]) { // Initializing the string String str = "Appdividend"; // Declaring another string String str1 = "Appdividend"; // str1 is the string String str2 = "appdividend"; // str2 is the string System.out.print("Difference between Appdividend and str1 is: "); // it will print 0 System.out.println(str.compareToIgnoreCase(str1)); System.out.print("Difference between Appdividend and str2 is: "); // it will print 0 System.out.println(str.compareToIgnoreCase(str2)); } }
See the output.
How to find length of string using String compareTo() method
Here we will see an interesting example of how to use the compareTo() method to find the length of the string.
If we compare the string with the empty string using the compareTo() method, then a method would return the length of the non-empty string.
class CompareToStr { public static void main(String args[]) { // Initializing the string String str1 = "Aaron Paul"; String str2 = ""; // empty string // it would return the length of str1 in positive number System.out.println(str1.compareTo(str2)); // it would return the length of str1 in negative number System.out.println(str2.compareTo(str1)); } }
See the output.
➜ java java CompareToStr 10 -10 ➜ java
In the above code example, the second compareTo() statement returned the length in a negative number because we have compared an empty string with str1. In the first compareTo() statement, we have compared str1 with an empty string.
Is Java String compareTo() method case sensitive
We compare the two strings using the compareTo() method in this example. Both the strings are the same; however, one of the strings is in uppercase, and the other string is in lowercase.
class CompareToStr { public static void main(String args[]) { //uppercase String str1 = "BREAKING BAD"; //lowercase String str2 = "breaking bad"; System.out.println(str1.compareTo(str2)); } }
See the output.
➜ java javac CompareToStr.java ➜ java java CompareToStr -32 ➜ java
As you can see that the output is not zero, which means that the compareTo() method is case sensitive. However, we have the case insensitive compare method in the String class.
That’s it for this tutorial.
Recommended Posts
How to find string length In Java
Java String toUpperCase()
Java Trim String
Java String valueOf()
Java String getchars()