-2

when i use this two methods, i want to know the difference,and how did equalsIgnoreCase() ignore the case of two String. But i even do not find the difference of the source code,only the order of code is different. who can help me analyze the difference of the source code,and how did it ignore case? thanks. here is the source code:

public static boolean equals(CharSequence cs1, CharSequence cs2) {
    if (cs1 == cs2) {
        return true;
    } else if (cs1 != null && cs2 != null) {
        if (cs1.length() != cs2.length()) {
            return false;
        } else {
            return cs1 instanceof String && cs2 instanceof String ? cs1.equals(cs2) : CharSequenceUtils.regionMatches(cs1, false, 0, cs2, 0, cs1.length());
        }
    } else {
        return false;
    }
}

public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) {
    if (str1 != null && str2 != null) {
        if (str1 == str2) {
            return true;
        } else {
            return str1.length() != str2.length() ? false : CharSequenceUtils.regionMatches(str1, true, 0, str2, 0, str1.length());
        }
    } else {
        return str1 == str2;
    }
}
muscleape
  • 3
  • 4

4 Answers4

0

equalsIgnoreCase performs the following tests:

  1. Is either string null? If so, return str1 == str2
  2. Does str1 == str2? If so, return true
  3. Do the lengths of the strings differ? If so, return false
  4. Compare the two using the regionMatches method of CharSequenceUtils, with the second argument, ignoreCase, set to true

You can find the source code for regionMatches here. It looks like that function upcases characters when comparing them if ignoreCase is set to true.

By contrast, equals invokes regionMatches with false for the ignoreCase argument, which will cause regionMatches to not upcase the characters.

chemicalcrux
  • 185
  • 10
0

equalsIgnoreCase(...) ignore the case (UPPER CASER o lower case). .so that

"HELLO".equalsIgnoreCase("hello")

Is true.

daN
  • 101
  • 5
0

the difference is in a method CharSequenceUtils.regionMatches . The second parameter is ignoreCase

static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart,
        CharSequence substring, int start, int length)    {
    if (cs instanceof String && substring instanceof String) {
        return ((String) cs).regionMatches(ignoreCase, thisStart, ((String) substring), start, length);
    } else {
        // TODO: Implement rather than convert to String
        return cs.toString().regionMatches(ignoreCase, thisStart, substring.toString(), start, length);
    }
}
Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
0

In equals you see String equality: Example:

 StringUtils.equals("abc", "abc") = true
 StringUtils.equals("abc", "ABC") = false

In equalsIgnoreCase as the name is saying try to compare string equality ignoring case:

 StringUtils.equalsIgnoreCase("abc", "abc") = true
 StringUtils.equalsIgnoreCase("abc", "ABC") = true 

From Apache Doc.

Gatusko
  • 2,503
  • 1
  • 17
  • 25