The compareToIgnoreCase
method of String
Class is implemented using the method in the snippet below(jdk1.8.0_45).
i. Why are both Character.toUpperCase(char)
and Character.toLowerCase(char)
used for comparison? Wouldn't either of them suffice the purpose of comparison?
ii. Why was s1.toLowerCase().compare(s2.toLowerCase())
not used to implement compareToIgnoreCase
? - I understand the same logic can be implemented in different ways. But, still I would like to know if there are specific reasons to choose one over the other.
public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
}
}
}
}
return n1 - n2;
}
Here's an example using Turkish i's:
System.out.println(Character.toUpperCase('i') == Character.toUpperCase('İ'));
System.out.println(Character.toLowerCase('i') == Character.toLowerCase('İ'));
The first line prints false
; the second true
. Ideone demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With