Java String ignore case implementation [duplicate]
Asked Answered
D

1

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;
  }
Dalliance answered 12/2, 2020 at 20:50 Comment(1)
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.Quail
P
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.

Poleyn answered 12/2, 2020 at 20:56 Comment(5)
What is toUpperCase of "ß" in Java?Dalliance
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.Poleyn
Isn't the capital of ß, SS or SZ (at least sometimes)? See here. Additionally System.out.println("ß".toUpperCase()); produces SS when I just tried it in Java.Pilkington
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?Dalliance
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.Poleyn

© 2022 - 2024 — McMap. All rights reserved.