Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string partial comparison [duplicate]

Possible Duplicate:
Why does string.Compare seem to handle accented characters inconsistently?

I have the following code

var s1 = "ABzzzzz2";
var s2 = "äbzzzzz1";

var cmp = StringComparison.InvariantCultureIgnoreCase;

Console.WriteLine(string.Compare(s1, 0, s2, 0, 7, cmp)); //prints -1
Console.WriteLine(string.Compare(s1, 0, s2, 0, 8, cmp)); //prints 1

How could it be that part of the first string is less than part of the second, while the whole first string is greater than the whole second one?
I've tested it on x64, .net 2.0, 3.5, 4.0

like image 819
alpha-mouse Avatar asked Nov 08 '12 13:11

alpha-mouse


1 Answers

My theory is that the algorithm first normalizes strings and then does the comparison. According to this "äbzzzzz1" normalized as "abzzzzz1". First comparison in normalized form results equality but returning 0 would be inccorent since the actual strings are not equal. So it reverts to ordinal comparison and results -1.

In the second case, after normalization, it is clear that "abzzzzz2" is greater than "abzzzzz1" so the result is 1.

This approach also explains cases mentioned in this question For normalization details check MSDN page

like image 151
Terry Cl. Avatar answered Nov 05 '22 02:11

Terry Cl.