In this tutorial, we’ll go through compareTo() Java method of the String class. Additionally, we’ll look into compareToIgnoreCase()
method and how it differs from compareTo()
.
1. What is String compareTo in Java?
The String
compareTo()
method can be used to compare two strings lexicographically based on the sum of the Unicode value of each character. This method is called by a string and has another string as a parameter.
2. How String compareTo Works in Java
The logic of the compareTo method is the following:
- If the two strings have the same length:
- For each character of each string, calculate the difference between the two Unicode values:
- Should the characters have the same value, proceed with the next one. If this is the last character, return 0.
- Should the characters have different value, return the difference between them.
- For each character of each string, calculate the difference between the two Unicode values:
- If the two strings have different length, e.g.
String a
has length 5 andString b
has length 8:- For the first 5 characters of each string:
- If the characters have the same value, proceed with the next one.
- If the characters have different value, return the difference between them.
- After comparing at most 5 characters we have these 2 cases:
- A different character was found, so the difference between the Unicode values was returned.
- The first 5 characters of the two strings are the same. In that case, the value to be returned will be the difference between the total characters of the first and the second string (in that case 5 – 8 = -3).
- For the first 5 characters of each string:
This means the following:
- If
String a
is “bigger” thanb
, it will return a positive number. - Should
String a
is equal tob
it will return 0. - If
String a
is “smaller” thanb
, it will return a negative number.
On the other hand, when it comes to compareToIgnoreCase()
, two characters will be compared in a case insensitive manner (this means that ‘A’ will be considered as having the same Unicode value as ‘a’).
2. String compareTo and compareToIgnoreCase Example
String clhMisspelled = "Code Learn Hab"; String clh = "Code Learn Hub"; String clhUpperCase = clh.toUpperCase(); String clhMixedCase = "CodE LeaRn HUb"; int codePointAtClh = clh.codePointAt(clh.length()-2); int codePointAtClhMisspelled = clhMisspelled.codePointAt(clhMisspelled.length()-2); System.out.println("Codepoint value of \"Code Learn Hub\" first different character: " + codePointAtClh); System.out.println("Codepoint value of \"Code Learn Hab\" first different character: " + codePointAtClhMisspelled); System.out.println("Difference between them: " + String.valueOf(codePointAtClh-codePointAtClhMisspelled)); System.out.println("\"Code Learn Hab\".compareTo(\"Code Learn Hab\") will return: " + clh.compareTo(clhMisspelled)); System.out.println(); System.out.println("\"Code Learn Hub\".compareTo(\"CODE LEARN HUB\") will return: " + clh.compareTo(clhUpperCase)); System.out.println("\"Code Learn Hub\".compareToIgnoreCase(\"CODE LEARN HUB\") will return: " + clh.compareToIgnoreCase(clhUpperCase)); System.out.println("\"Code Learn Hub\".compareToIgnoreCase(\"CodE LeaRn HUb\") will return: " + clh.compareToIgnoreCase(clhMixedCase));
- Lines 5-6: We calculate the Unicode value of the first character that is different between the two Strings (that is the ‘a’).
- Lines 12-13: We print the difference between the two Unicode values.
- Line 14-15: We print the result of
clh.compareTo(clhMisspelled)
which is the same as the result in line 9.
The output will be the following:
Codepoint value of "Code Learn Hub" first different character: 117 Codepoint value of "Code Learn Hab" first different character: 97 Difference between them: 20 "Code Learn Hab".compareTo("Code Learn Hab") will return: 20 "Code Learn Hab".compareTo("CODE LEARN HUB") will return: 32 "Code Learn Hab".compareToIgnoreCase("CODE LEARN HUB") will return: 0 "Code Learn Hab".compareToIgnoreCase("CodE LeaRn HUb") will return: 0
3. Conclusion
By now, you should be able to use the compareTo()
and compareToIgnoreCase()
methods in the most efficient way. You can find the source code on our GitHub page.