Reputation: 3269
I thought StringComparison.OrdinalIgnoreCase
and StringComparison.InvariantCultureIgnoreCase
do the same job when it comes to English-only strings. However it's not the case in the following code that I'm working on:
// Returns 0
string.Compare("877495169FA05B9D8639A0EBC42022338F7D2324","877495169fa05b9d8639a0ebc42022338f7d2324", StringComparison.InvariantCultureIgnoreCase)
// Returns -1
string.Compare("877495169FA05B9D8639A0EBC42022338F7D2324","877495169fa05b9d8639a0ebc42022338f7d2324", StringComparison.OrdinalIgnoreCase)
Is there a particular reason why?
Upvotes: 12
Views: 7844
Reputation: 942000
"877495169fa05b9d8639a0ebc42022338f7d2324"
Sounds like a trick question. There's an extra character at the start at this string, before the first digit 8. It isn't visible in the browser. It is U+200E, "Left to Right Mark". The ordinal comparison sees that character, the invariant comparison ignores it. You can see it for yourself by using ToCharArray() on the string.
Delete that string and paste this one instead, I removed U+200E from it:
"877495169fa05b9d8639a0ebc42022338f7d2324"
And the Compare() method now returns 0 like it should. Do watch out for that text editor or IME you are using right now. Isn't Unicode fun?
Upvotes: 26