. Advertisement .
..3..
. Advertisement .
..4..
You can use methods compareTo()
and compareToIgnoreCase()
to compare strings alphabetically in Java. Continue reading to learn how they work.
Compare Strings Alphabetically In Java
Using compareTo()
The method compareTo()
belongs to the string class. It compares two strings in dictionary order (lexicographically) and returns an integer value.
What it does is compare the Unicode value of each character in the two given strings. If they are equal, meaning they have the same length and have the same character in every position, the method returns 0.
When the method compareTo()
encounters the first pair of different characters, they are used to determine which string comes first. The comparison is done using the Unicode character set. If the first string comes before the second one in lexicographical order, it returns a negative value. Otherwise, a positive value will be returned.
There are multiple ways to put this method to use. For instance, this is a basic syntax of the method compareTo()
that you can use to compare the strings str1 and str2:
Example:
public class ITtutoria {
public static void main(String args[])
{
// Initializing Three Strings
String str1 = "ITTutoria";
String str2 = new String("ITTutoria");
String str3 = new String("Stack Overflow");
// Checking if ITTutoria string
// equates to ITTutoria object
System.out.print(
"Comparing ITTutoria(obj) with ITTutoria(str) : ");
System.out.println(str1.compareTo(str2));
// Checking if ITTutoria string
// equates to Stack Overflow object
System.out.print(
"Comparing of ITTutoria(obj) with Stack Overflow(str) : ");
System.out.println(str1.compareTo(str3));
}
}
Output:
Comparing ITTutoria(obj) with ITTutoria(str) : 0
Comparing of ITTutoria(obj) with Stack Overflow(str) : -10
In the example above, we first create three strings. Two of them have the same value but are created in different ways: one from a string literal, one by a constructor with the new keyword.
The two statements that compare the first string with two other strings:
str1.compareTo(str2)
str1.compareTo(str3)
The first line produces the value 0. This means, regardless of how they are created, those two strings are equal. On the other hand, the second comparison returns a negative value (-10), indicating the first string comes before the second one. It is easy to see why if you consider their first character (‘I’ and ‘S’).
You can use these outputs for more complicated tasks, such as if-else statements like this:
import java.util.Scanner;
public class ITtutoria {
public static void main(String args[])
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter the first string");
String str1 = myObj.nextLine();
System.out.println("Enter the second string");
String str2 = myObj.nextLine();
if (str1.compareTo(str2) == 0) {
System.out.println("The two strings are equal.");
}
else {
System.out.println("The two strings are not equal.");
}
}
}
Output:
Enter the first string
Python
Enter the second string
Java
The two strings are not equal.
This program asks for two strings from the user. It then compares them with the method compareTo()
and prints a result message.
Using compareToIgnoreCase()
This method works mostly like compareTo()
. The only difference is that it ignores the case of all characters of two strings while comparing them.
This is a simple example demonstrating their difference:
public class ITtutoria {
public static void main(String args[])
{
// Initializing Two Strings
String str1 = "ITTutoria";
String str2 = "ITTUTORIA";
// Checking if ITTutoria string
// equates to ITTUTORIA string
System.out.print(
"Comparing ITTutoria with ITTUTORIA" + '\n');
System.out.print(
"with compareTo(): ");
System.out.println(str1.compareTo(str2));
System.out.print(
"with compareToIgnoreCase(): ");
System.out.println(str1.compareToIgnoreCase(str2));
}
}
Output:
Comparing ITTutoria with ITTUTORIA
with compareTo(): 32
with compareToIgnoreCase(): 0
As you can see, the method compareTo()
tells us that the second string comes before the first string. It makes sense because in the Unicode character set, upper case letters come before lowercase ones.
But the method compareToIgnoreCase()
doesn’t care about the case and will show the value 0. This is the expected result when you want to ignore letter cases.
Conclusion
The methods compareTo()
and compareToIgnoreCase()
can compare strings alphabetically in Java. They are almost identical, except for the fact that one of them takes letter cases into account while the other doesn’t. And if you want to compare integers, read this guide.
Leave a comment