You may want to compare two strings to know which is higher or lower alphabetically or to see if they are equal. You can
do this in many ways. I'll show you two of them in this article. You can use the Here are some examples comparing two strings: It gives Another example: The comparison above gives One more example: Comparing
"fcc" and "fcc" gives In some browsers, instead of -1, it may return -2 or some other negative value. So, do not depend on -1 or 1, instead on negative (less than 0) or positive (more than 0) values You can also use mathematical operators like greater than (>), less than (<), and equal to when comparing strings. Mathematical operators work similarly to Using the previous examples: For the other example: And for the last example: With mathematical operators, "fcc" is greater than "Fcc", but with This behavior is one reason why I don't recommend using mathematical operators for comparing strings, even though it has the potential to do so. Another reason why I don't recommend using mathematical operators is because So, for comparing strings, amongst the many ways there may be, using Now you know an easy way to compare strings. Happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well. The String class of Java comprises a lot of methods to execute various operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), substring() etc. Out of these methods, we will be focusing on the compareTo() method. String.compareTo() MethodThe Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, negative number, or 0. It compares strings on the basis of the Unicode value of each character in the strings. If the first string is lexicographically greater than the second string, it returns a positive number (difference of character value). If the first string is less than the second string lexicographically, it returns a negative number, and if the first string is lexicographically equal to the second string, it returns 0.
There are three variants of the compareTo() method. This article depicts all of them, as follows 1. int compareTo(Object obj)This method compares this String to another Object. Syntax: int compareTo(Object obj) Parameters:
Return Value: The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string. Java
Output Difference of geeksforgeeks(obj) and geeksforgeeks(str) : 0 Difference of astha(obj) and geeksforgeeks(str) : 6 2. int compareTo(String anotherString)This method compares two strings lexicographically. Syntax: int compareTo(String anotherString) Parameters:
Return Value: The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string. Java
Output Difference of geeksforgeeks(str) and geeksforgeeks(str) : 0 Difference of astha(str) and geeksforgeeks(str) : 6 3. int compareToIgnoreCase(String str)This method compares two strings lexicographically, ignoring case differences. Syntax: int compareToIgnoreCase(String str) Parameters:
Return Value: This method returns a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations. Java
Output Difference of geeks and gEEkS (case sensitive) : 32 Difference of geeks and gEEkS (case insensitive) : 0 This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . 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 |