Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.CompareTo with case

Tags:

c#

.net

Sample code to illustrate:

    int res1 = "a".CompareTo("A");  // res1 = -1
    int res2 = "ab".CompareTo("A");  // res2 = 1

I'm seeing res1 = -1, and res2 = 1 at the end, which was a bit unexpected. I thought res1 would return 1, since on an ASCII chart "A" (0x41) comes before "a" (0x61).

Also, it seems strange that for res2, the length of the string seems to make a difference. i.e. if "a" comes before "A" (as res1 = -1 indicates), then I would have thought that "a"withAnythingAfterIt would also come before "A"withAnythingAfterIt.

Can someone shed some light? Thanks.

like image 948
Moe Sisko Avatar asked Apr 05 '18 03:04

Moe Sisko


People also ask

Is string compareTo case sensitive?

The String. CompareTo instance methods always perform an ordinal case-sensitive comparison. They are primarily suited for ordering strings alphabetically.

Does compareTo ignore case?

The compareToIgnoreCase() method compares two strings lexicographically, ignoring lower case and upper case differences. The comparison is based on the Unicode value of each character in the string converted to lower case. The method returns 0 if the string is equal to the other string, ignoring case differences.

Does compareTo ignore case Java?

The Java String compareTo() method compares two strings lexicographically (in the dictionary order), ignoring case differences.

How does compareTo work with strings?

The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.


1 Answers

This is the expected behavior. String.CompareTo(string) does a culture sensitive comparison, using its sort order. In fact it calls CultureInfo to do the job as we can see in the source code:

public int CompareTo(String strB) {
    if (strB==null) {
        return 1;
    }

    return CultureInfo.CurrentCulture.CompareInfo.Compare(this, strB, 0);
}

Your current culture puts 'A' after 'a' in the sort order, since it would be a tie, but not after 'ab' since clearly 'ab' comes after either 'a' or 'A' in most sort orders I know. It's just the tie breaking mechanism doing its work: when the sort order would be the same, use the ordinal value!

like image 127
Loudenvier Avatar answered Nov 06 '22 07:11

Loudenvier