Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String ignore case implementation [duplicate]

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;
  }
like image 250
N3al Avatar asked Feb 12 '20 20:02

N3al


People also ask

Does Java String contains Ignore case?

That’s all about Java String contains ignore case. How to Print to File in Python? Remove nan From List In Python?

How to implement ignore case in regex?

Case-insensitivity is defined as by String.equalsIgnoreCase (String). A null CharSequence will return false. This one will be better than regex as regex is always expensive in terms of performance. you can implement your own custom containsIgnoreCase using java.lang.String.regionMatches ignoreCase : if true, ignores case when comparing characters.

How to implement custom containsignorecase in Java?

you can implement your own custom containsIgnoreCase using java.lang.String.regionMatches ignoreCase : if true, ignores case when comparing characters.

How do you use equalsignorecase?

Definition and Usage The equalsIgnoreCase () method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase () method to compare two strings lexicographically, ignoring case differences.


1 Answers

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.

like image 114
Stefan Avatar answered Oct 16 '22 05:10

Stefan