A.D.
A.D.

Reputation: 1116

odd results when comparing strings based on culture

is there a reason why :

string s1 = "aéa"; string s2 = "aea";

string result = s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
result = s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase);

result = false in all cases although my current culture is french. I would expect one of the 2 lines should return true?

On the other hand, I get

int a = string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);

a = 0 meaning an equality.

This sounds paradoxal to me. Any explanation???

thx in advance.

Upvotes: 0

Views: 234

Answers (1)

onatm
onatm

Reputation: 846

In the first equality check, you are ignoring case with StringComparison.CurrentCultureIgnoreCase in your current culture (fr). So, first check should be false.

In the second one, you are ignoring case in invariant culture with StringComparison.InvariantCultureIgnoreCase. é is not equal to e in invariant culture. Those characters are in fact different (has different meaning) in most cultures. This check should be false.

In the last one, you are ignoring characters, such as diacritics, with CompareOptions.IgnoreNonSpace. The last one should be true.

Also, read here.

Upvotes: 1

Related Questions