|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How To Compare Two Strings Lexicographically In Java" |
| 4 | +author: gaurav |
| 5 | +categories: [ Java, Core Java, String] |
| 6 | +description: "In this article, we will learn how to compare two strings lexicographically in java." |
| 7 | +featured: false |
| 8 | +comments: true |
| 9 | +hidden: false |
| 10 | +--- |
| 11 | + |
| 12 | +In this article, we will learn how to compare two strings lexicographically in java. |
| 13 | + |
| 14 | +## Introduction |
| 15 | + |
| 16 | +First of all, we will understand what does ‘lexicographically’ means? |
| 17 | + |
| 18 | +In simple words ‘lexicographically’ means ‘alphabetically ordered’. Yes, correct. The order in which words are given in the dictionary. |
| 19 | + |
| 20 | +We are going to compare two strings so we can check their lexicographical order. |
| 21 | + |
| 22 | +There are two ways to compare two strings lexicographically |
| 23 | +1. Using the Java `compareTo()` method |
| 24 | +2. By creating a user-defined method |
| 25 | + |
| 26 | +Let us start with the first option |
| 27 | + |
| 28 | +## 1. Using the Java `compareTo()` method |
| 29 | +Java `compareTo()` method Compares two strings lexicographically, The comparison is based on the Unicode value of each character in the strings. |
| 30 | + |
| 31 | +The character sequence represented by the String object is compared lexicographically to the character sequence represented by the argument string. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +```java |
| 36 | +int compareTo(T o) |
| 37 | +where is 'T' is type and 'o' is obejct |
| 38 | +returns - a negative integer, 0 or a positive integer |
| 39 | + |
| 40 | +ex. firstString.compareTo(secondString) |
| 41 | + |
| 42 | +// here character sequence of the firstString is compared lexicographically with the character sequence of the secondString |
| 43 | +``` |
| 44 | +`compareTo()` returns the integer (`int`) value. The possible values are a negative integer, zero or a positive integer. |
| 45 | + |
| 46 | +1. If `firstString` is less than the `secondString`, it will return a negative integer. |
| 47 | +i.e `firstString` < `secondString`→ returns a negative integer |
| 48 | + |
| 49 | +2. If `firstString` is equal to the `secondString` it will return zero. |
| 50 | +i.e `firstString` == `secondString` → returns zero |
| 51 | + |
| 52 | +3. If `firstString` is greater than the `secondString` it will return a positive integer. |
| 53 | +i.e `firstString` > `secondString` → returns a positive integer |
| 54 | + |
| 55 | +Note: Always consider ‘Argument string’ as the reference to counting form for the sign of the value. |
| 56 | + |
| 57 | +Java program to check two strings `lexicographically` using Java `compareTo()` method |
| 58 | +```java |
| 59 | +/** |
| 60 | + * A Java program to compare two strings lexicographically |
| 61 | + * using compareTo() library function. |
| 62 | + * |
| 63 | + * @author Gaurav Kukade at coderolls.com |
| 64 | + */ |
| 65 | +public class CompareLexicographically { |
| 66 | + public static void main(String[] args) { |
| 67 | + |
| 68 | + String firstString = "Paneer"; |
| 69 | + String secondString = "Paneer"; |
| 70 | + String thirdString = "Butter"; |
| 71 | + String fourthString = "Cheese"; |
| 72 | + |
| 73 | + System.out.println("Comparing two strings lexicographically."); |
| 74 | + |
| 75 | + System.out.print("\nCompairing character sequence of the firstString ("+firstString+") to the character sequence of the secondString ("+secondString+") returns: "); |
| 76 | + System.out.println(firstString.compareTo(secondString)); |
| 77 | + |
| 78 | + System.out.print("\nCompairing character sequence of secondString ("+secondString+") to the character sequence of thirdString ("+thirdString+") returns: "); |
| 79 | + System.out.println(secondString.compareTo(thirdString)); |
| 80 | + |
| 81 | + System.out.print("\nCompairing character sequence of thirdString ("+thirdString+") to the character sequence of fourthString ("+fourthString+") returns: "); |
| 82 | + System.out.println(thirdString.compareTo(fourthString)); |
| 83 | + |
| 84 | + System.out.print("\nCompairing character sequence of fourthString ("+fourthString+") to the character sequence of firstString ("+firstString+") returns: "); |
| 85 | + System.out.println(fourthString.compareTo(firstString)); |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | +View CompareLexicographically.java as GitHub Gist. |
| 90 | + |
| 91 | +Output : |
| 92 | + |
| 93 | +```java |
| 94 | +Comparing two strings lexicographically. |
| 95 | + |
| 96 | +Compairing character sequence of the firstString (Paneer) to the character sequence of the secondString (Paneer) returns: 0 |
| 97 | + |
| 98 | +Compairing character sequence of secondString (Paneer) to the character sequence of thirdString (Butter) returns: 14 |
| 99 | + |
| 100 | +Compairing character sequence of thirdString (Butter) to the character sequence of fourthString (Cheese) returns: -1 |
| 101 | + |
| 102 | +Compairing character sequence of fourthString (Cheese) to the character sequence of firstString (Paneer) returns: -13 |
| 103 | +``` |
| 104 | +Explanation: |
| 105 | +1. In first case, compareTo() method returns zero since firstString and secondString are same. |
| 106 | +2. In second case, compareTo() method returns 14 since secondString follows thirdString by 14 characters. The pictorial explanation for this case is given below. |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +3. In third case, compareTo() method returns -1 since thirdString precedes fourthString by 1 character. |
| 111 | +4. In last case, compareTo() method returns -13 since fourthString precedes firstString by 13 characters. The pictorial explanation for this case is given below. |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## 2. By creating a user-defined method |
| 116 | + |
| 117 | +You can create a method that will compare two strings lexicographically. |
| 118 | + |
| 119 | +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. |
| 120 | + |
| 121 | +1. Get the length of the shorter string in an integer variable lim. |
| 122 | + |
| 123 | +2. Apply while loop for condition `k`<`lim` where k is integer variable initiated with 0. |
| 124 | + |
| 125 | +3. 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. |
| 126 | + |
| 127 | +We will cast the difference as integer value so that the difference between the Unicode value of character will be return. |
| 128 | + |
| 129 | +4. 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`. |
| 130 | + |
| 131 | +5. If the if condition is false for all iterations, return the difference between two strings. |
| 132 | + |
| 133 | +I have given the program with a user-defined method `comapreString` below |
| 134 | +```java |
| 135 | +/** |
| 136 | + * The Java program to compare two strings lexicographically |
| 137 | + * by creating user defined function. |
| 138 | + * |
| 139 | + * @author Gaurav Kukade at coderolls.com |
| 140 | + */ |
| 141 | +public class CompareLexicographicallyWithUserDefinedFunction { |
| 142 | + public static void main(String[] args) { |
| 143 | + |
| 144 | + String firstString = "Paneer"; |
| 145 | + String secondString = "Paneer"; |
| 146 | + String thirdString = "Butter"; |
| 147 | + String fourthString = "Cheese"; |
| 148 | + String fifthString = "PaneerButter"; |
| 149 | + |
| 150 | + System.out.println("Comparing two strings lexicographically by user defined function"); |
| 151 | + |
| 152 | + System.out.print("\nCompairing firstString ("+firstString+") to the secondString ("+secondString+") returns: "); |
| 153 | + System.out.println(compareString(firstString, secondString)); |
| 154 | + |
| 155 | + System.out.print("\nCompairing secondString ("+secondString+") to the thirdString ("+thirdString+") returns: "); |
| 156 | + System.out.println(compareString(secondString, thirdString)); |
| 157 | + |
| 158 | + System.out.print("\nCompairing thirdString ("+thirdString+") to the fourthString ("+fourthString+") returns: "); |
| 159 | + System.out.println(compareString(thirdString, fourthString)); |
| 160 | + |
| 161 | + System.out.print("\nCompairing fourthString ("+fourthString+") to the firstString ("+firstString+") returns: "); |
| 162 | + System.out.println(compareString(fourthString, firstString)); |
| 163 | + |
| 164 | + // Edge case comparing Paneer & PaneerButter |
| 165 | + System.out.print("\nCompairing firstString ("+firstString+") to the fifthString ("+fifthString+") returns: "); |
| 166 | + System.out.println(compareString(firstString, fifthString)); |
| 167 | + } |
| 168 | + |
| 169 | + /* |
| 170 | + * User defined function to compare two string lexicographically |
| 171 | + */ |
| 172 | + |
| 173 | + public static int compareString(String str, String argumentString) { |
| 174 | + |
| 175 | + int lim= Math.min(str.length(), argumentString.length()); |
| 176 | + |
| 177 | + int k=0; |
| 178 | + while(k<lim) { |
| 179 | + if(str.charAt(k)!= argumentString.charAt(k)) { |
| 180 | + return (int) str.charAt(k)- argumentString.charAt(k); |
| 181 | + } |
| 182 | + k++; |
| 183 | + } |
| 184 | + return str.length() - argumentString.length(); |
| 185 | + } |
| 186 | +} |
| 187 | +``` |
| 188 | +Output: |
| 189 | +```java |
| 190 | +Comparing two strings lexicographically by user defined function |
| 191 | + |
| 192 | +Compairing firstString (Paneer) to the secondString (Paneer) returns: 0 |
| 193 | + |
| 194 | +Compairing secondString (Paneer) to the thirdString (Butter) returns: 14 |
| 195 | + |
| 196 | +Compairing thirdString (Butter) to the fourthString (Cheese) returns: -1 |
| 197 | + |
| 198 | +Compairing fourthString (Cheese) to the firstString (Paneer) returns: -13 |
| 199 | + |
| 200 | +Compairing firstString (Paneer) to the fifthString (PaneerButter) returns: -6 |
| 201 | +``` |
| 202 | +## Conclusion |
| 203 | +We have seen how to compare two strings lexicographically in Java. As per the articles, there are two ways to do the same |
| 204 | +1. Using the Java compareTo() method |
| 205 | +2. By creating a user-defined method |
| 206 | + |
| 207 | +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(). |
| 208 | + |
| 209 | +Most noteworthy, In compareTo() method of the Java, the comparison is based on the Unicode value of each character in the strings. |
| 210 | + |
| 211 | +If you found this article worth, please [Give me a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 212 | + |
| 213 | +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. |
0 commit comments