6

I was reading java.lang.String equals ignore case implementation and trying figure out why is there a lower case compare after upper case is already compared? Are there languages where this matters ,where upper cases may not match but lower cases may match?

// Code from java.lang.String class 
  public boolean regionMatches(boolean paramBoolean, int paramInt1, String paramString, int paramInt2, int paramInt3) {
    char[] arrayOfChar1 = this.value;
    int i = paramInt1;
    char[] arrayOfChar2 = paramString.value;
    int j = paramInt2;
    if (paramInt2 < 0 || paramInt1 < 0 || paramInt1 > this.value.length - paramInt3 || paramInt2 > paramString.value.length - paramInt3)
      return false; 
    while (paramInt3-- > 0) {
      char c1 = arrayOfChar1[i++];
      char c2 = arrayOfChar2[j++];
      if (c1 == c2)
        continue; 
      if (paramBoolean) {
        char c3 = Character.toUpperCase(c1);
        char c4 = Character.toUpperCase(c2);
// Why is java comparing to Lower case here
        if (c3 == c4 || Character.toLowerCase(c3) == Character.toLowerCase(c4))
          continue; 
      } 
      return false;
    } 
    return true;
  }
N3al
  • 171
  • 1
  • 6
  • Not sure whether there IS such a language - the German ß/ss business and the Turkish variations of i don't seem to count. But there _could_ be, now or in future, so I imagine the makers of Java just wanted to be extra sure that their algorithm would be right. – Dawood ibn Kareem Feb 12 '20 at 21:10

1 Answers1

3

Some characters exist only in lower case, some only exist in upper case. For example, in Germany we have the character "ß" which is lower case. There is no upper case version of it.

I assume that the same can happen in the opposite direction in other languages.

Slaw
  • 37,820
  • 8
  • 53
  • 80
Stefan
  • 1,789
  • 1
  • 11
  • 16
  • What is toUpperCase of "ß" in Java? – N3al Feb 12 '20 at 20:58
  • 1
    There is no uppercase representation of "ß". It simply does not exist. And this is not a Java issue, it affects all programming languages. In Java ```"ß".toUpperCase()``` returns "SS". Other programming languages might return "ß" or nothing. – Stefan Feb 12 '20 at 21:01
  • Isn't the capital of ß, SS or SZ (at least sometimes)? See [here](https://en.wikipedia.org/wiki/Capital_%E1%BA%9E). Additionally `System.out.println("ß".toUpperCase());` produces `SS` when I just tried it in Java. – Nexevis Feb 12 '20 at 21:01
  • So in that case lets say there is character "x" which like "ß" has no upper case then wouldnt upper of "ß" be equal to upper of "x" even though they are not same just like. null == null or SS == SS and really be wrong comparison? – N3al Feb 12 '20 at 21:42
  • I am not sure if I understood your question properly. The uppercase of 'ß' is 'SS', which is not totally wrong because we germans write 'ss' instead of 'ß' on devices that do not have an 'ß' on the keyboard. We also write 'oe' when there is no 'ö'. The code above would say "Faß" equals "Fass", however ```String.compareToIgnoreCase()``` returns 108 (no match). I cannot say which one is better. – Stefan Feb 13 '20 at 16:56