How To Compare Two Strings Lexicographically In Java
In this article, we will learn how to compare two strings lexicographically in java.
Introduction
First of all, we will understand what does âlexicographicallyâ means?
In simple words âlexicographicallyâ means âalphabetically orderedâ. Yes, correct. The order in which words are given in the dictionary.
We are going to compare two strings so we can check their lexicographical order.
There are two ways to compare two strings lexicographically
- Using the Java
compareTo()
method - By creating a user-defined method
Let us start with the first option
1. Using the Java compareTo()
method
Java compareTo()
method Compares two strings lexicographically, The comparison is based on the Unicode value of each character in the strings.
The character sequence represented by the String object is compared lexicographically to the character sequence represented by the argument string.
1
2
3
4
5
6
7
int compareTo(T o)
where is 'T' is type and 'o' is obejct
returns - a negative integer, 0 or a positive integer
ex. firstString.compareTo(secondString)
// here character sequence of the firstString is compared lexicographically with the character sequence of the secondString
compareTo()
returns the integer (int
) value. The possible values are a negative integer, zero or a positive integer.
-
If
firstString
is less than thesecondString
, it will return a negative integer. i.efirstString
<secondString
â returns a negative integer -
If
firstString
is equal to thesecondString
it will return zero. i.efirstString
==secondString
â returns zero -
If
firstString
is greater than thesecondString
it will return a positive integer. i.efirstString
>secondString
â returns a positive integer
Note: Always consider âArgument stringâ as the reference to counting form for the sign of the value.
Java program to check two strings lexicographically
using Java compareTo()
method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* A Java program to compare two strings lexicographically
* using compareTo() library function.
*
* @author Gaurav Kukade at coderolls.com
*/
public class CompareLexicographically {
public static void main(String[] args) {
String firstString = "Paneer";
String secondString = "Paneer";
String thirdString = "Butter";
String fourthString = "Cheese";
System.out.println("Comparing two strings lexicographically.");
System.out.print("\nCompairing character sequence of the firstString ("+firstString+") to the character sequence of the secondString ("+secondString+") returns: ");
System.out.println(firstString.compareTo(secondString));
System.out.print("\nCompairing character sequence of secondString ("+secondString+") to the character sequence of thirdString ("+thirdString+") returns: ");
System.out.println(secondString.compareTo(thirdString));
System.out.print("\nCompairing character sequence of thirdString ("+thirdString+") to the character sequence of fourthString ("+fourthString+") returns: ");
System.out.println(thirdString.compareTo(fourthString));
System.out.print("\nCompairing character sequence of fourthString ("+fourthString+") to the character sequence of firstString ("+firstString+") returns: ");
System.out.println(fourthString.compareTo(firstString));
}
}
View CompareLexicographically.java as GitHub Gist.
Output :
1
2
3
4
5
6
7
8
9
Comparing two strings lexicographically.
Compairing character sequence of the firstString (Paneer) to the character sequence of the secondString (Paneer) returns: 0
Compairing character sequence of secondString (Paneer) to the character sequence of thirdString (Butter) returns: 14
Compairing character sequence of thirdString (Butter) to the character sequence of fourthString (Cheese) returns: -1
Compairing character sequence of fourthString (Cheese) to the character sequence of firstString (Paneer) returns: -13
Explanation:
- In first case,
compareTo()
method returns zero since firstString and secondString are same. -
In second case,
compareTo()
method returns 14 since secondString follows thirdString by 14 characters. The pictorial explanation for this case is given below. - In third case,
compareTo()
method returns -1 since thirdString precedes fourthString by 1 character. - In last case,
compareTo()
method returns -13 since fourthString precedes firstString by 13 characters. The pictorial explanation for this case is given below.
2. By creating a user-defined method
You can create a method that will compare two strings lexicographically.
First, we will see the logic, how can we build the logic for our user-defined method. I have given the step by step logic below.
-
Get the length of the shorter string in an integer variable lim.
-
Apply while loop for condition
k
<lim
where k is integer variable initiated with 0. -
Apply if condition to check if the character at an index k of both the strings is not similar; if the condition returns the difference between these two characters.
We will cast the difference as integer value so that the difference between the Unicode value of character will be return.
-
If the if condition is false, the while loop will continue for the rest of the iterations until condition is true i.e
k
<lim
. -
If the if condition is false for all iterations, return the difference between two strings.
I have given the program with a user-defined method comapreString
below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* The Java program to compare two strings lexicographically
* by creating user defined function.
*
* @author Gaurav Kukade at coderolls.com
*/
public class CompareLexicographicallyWithUserDefinedFunction {
public static void main(String[] args) {
String firstString = "Paneer";
String secondString = "Paneer";
String thirdString = "Butter";
String fourthString = "Cheese";
String fifthString = "PaneerButter";
System.out.println("Comparing two strings lexicographically by user defined function");
System.out.print("\nCompairing firstString ("+firstString+") to the secondString ("+secondString+") returns: ");
System.out.println(compareString(firstString, secondString));
System.out.print("\nCompairing secondString ("+secondString+") to the thirdString ("+thirdString+") returns: ");
System.out.println(compareString(secondString, thirdString));
System.out.print("\nCompairing thirdString ("+thirdString+") to the fourthString ("+fourthString+") returns: ");
System.out.println(compareString(thirdString, fourthString));
System.out.print("\nCompairing fourthString ("+fourthString+") to the firstString ("+firstString+") returns: ");
System.out.println(compareString(fourthString, firstString));
// Edge case comparing Paneer & PaneerButter
System.out.print("\nCompairing firstString ("+firstString+") to the fifthString ("+fifthString+") returns: ");
System.out.println(compareString(firstString, fifthString));
}
/*
* User defined function to compare two string lexicographically
*/
public static int compareString(String str, String argumentString) {
int lim= Math.min(str.length(), argumentString.length());
int k=0;
while(k<lim) {
if(str.charAt(k)!= argumentString.charAt(k)) {
return (int) str.charAt(k)- argumentString.charAt(k);
}
k++;
}
return str.length() - argumentString.length();
}
}
Output:
1
2
3
4
5
6
7
8
9
10
11
Comparing two strings lexicographically by user defined function
Compairing firstString (Paneer) to the secondString (Paneer) returns: 0
Compairing secondString (Paneer) to the thirdString (Butter) returns: 14
Compairing thirdString (Butter) to the fourthString (Cheese) returns: -1
Compairing fourthString (Cheese) to the firstString (Paneer) returns: -13
Compairing firstString (Paneer) to the fifthString (PaneerButter) returns: -6
Conclusion
We have seen how to compare two strings lexicographically in Java. As per the articles, there are two ways to do the same
- Using the Java
compareTo()
method - By creating a user-defined method
In a first way, I am using the compareTo()
method of the Java and in the second way I have created the user-defined method compareToString()
.
Most noteworthy, In compareTo()
method of the Java, the comparison is based on the Unicode value of each character in the strings.
Have you tried the compareTo()
method or created a user-defined method to compare two string lexicographically? How was your experience? Have you faced any problem? please write down the same in the comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!