You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/java-string/2019-11-30-compare-two-strings-lexicographically-in-java.md
+9-10Lines changed: 9 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -102,13 +102,12 @@ Compairing character sequence of thirdString (Butter) to the character sequence
102
102
Compairing character sequence of fourthString (Cheese) to the character sequence of firstString (Paneer) returns:-13
103
103
```
104
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
-
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.
108
107

109
108
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.
109
+
3. In third case, `compareTo()` method returns -1 since thirdString precedes fourthString by 1 character.
110
+
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
111
113
112

114
113
@@ -123,8 +122,8 @@ First, we will see the logic, how can we build the logic for our user-defined me
123
122
2. Apply while loop for condition `k`<`lim` where k is integer variable initiated with 0.
124
123
125
124
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.
125
+
126
+
We will cast the difference as integer value so that the difference between the Unicode value of character will be return.
128
127
129
128
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
129
@@ -201,12 +200,12 @@ Compairing firstString (Paneer) to the fifthString (PaneerButter) returns: -6
201
200
```
202
201
## Conclusion
203
202
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
203
+
1. Using the Java `compareTo()` method
205
204
2. By creating a user-defined method
206
205
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().
206
+
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
207
209
-
Most noteworthy, In compareTo() method of the Java, the comparison is based on the Unicode value of each character in the strings.
208
+
Most noteworthy, In `compareTo()` method of the Java, the comparison is based on the Unicode value of each character in the strings.
210
209
211
210
If you found this article worth, please [Give me a cup of Coffee ☕](https://www.paypal.me/GauravKukade)
0 commit comments