In this article, we will discuss how we can compare two strings lexicographically in Java.
One solution is to use Java compareTo() method. The method compareTo() is used for comparing two strings lexicographically in Java. Each character of both the strings is converted into a Unicode value for comparison.
int compareTo(String str) :
It returns the following values:
- if (string1 > string2) it returns a positive value.
- if both the strings are equal lexicographically
i.e.(string1 == string2) it returns 0.
- if (string1 < string2) it returns a negative value.
Comparing strings with compareTo: 0 -1 17
Refer How to Initialize and Compare Strings in Java? for more details.
How to compare two strings without using library function?
1. Input two strings string 1 and string 2. 2. for (int i = 0; i < str1.length() && i < str2.length(); i ++) (Loop through each character of both strings comparing them until one of the string terminates): a. If unicode value of both the characters is same then continue; b. If unicode value of character of string 1 and unicode value of string 2 is different then return (str1[i]-str2[i]) 3. if length of string 1 is less than string2 return str2[str1.length()] else return str1[str2.length()]
Below is the implementation of the above algorithm.
-9 0 9 -8 8
This article is contributed by Ankit Jain . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Count of same length Strings that exists lexicographically in between two given Strings
- Compare two Strings in Java
- Compare two strings considering only alphanumeric characters
- C++ program to compare two Strings using Operator Overloading
- C program to Compare Two Strings without using strcmp() function
- How to Initialize and Compare Strings in Java?
- How to compare two arrays in Java?
- How to Compare two Collections in Java?
- Compare two Version numbers
- C++ Program to compare two string using pointers
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Lexicographically smallest string which differs from given strings at exactly K indices
- Sort an array of strings lexicographically based on prefix
- Count lexicographically increasing K-length strings possible from first N alphabets
- Count N-length strings consisting only of vowels sorted lexicographically
- Java Integer compare() method
- Short compare() method in Java
- Boolean compare() method in Java with Examples
- Byte compare() method in Java with examples
- Compare Dates in Java