0

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.

A.D.
  • 1,062
  • 1
  • 13
  • 37
  • You may refer to this url for some help.http://stackoverflow.com/questions/2876237/problem-comparing-french-character-%C3%8E – Sudipta Kumar Sahoo Mar 14 '16 at 18:44
  • Your first two comparisons are ignoring case, while your 3rd comparison is ignoring [nonspacing combining characters](https://msdn.microsoft.com/en-us/library/system.globalization.compareoptions%28v=vs.110%29.aspx) (which include the acute accent), so it seems logical that they would produce different results. If you use `CompareOptions.IgnoreCase` I would guess that the results will match your first comparison. – Mark Mar 14 '16 at 19:27
  • @Mark The case is the same between "aéa" and "aea". So the comparison with CurrentCultureIgnoreCase should return true from my point of view? I m looking for the different methods returning an equality for these strings. – A.D. Mar 15 '16 at 08:20

1 Answers1

1

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.

onatm
  • 816
  • 1
  • 11
  • 29
  • OK, I see, I thought I oculd get an equality result for é and e based on StringComparison enum, it looks not. I ll figure out a solution with CompareOptions.IgnoreNonSpace then. Thank you for your time. – A.D. Mar 15 '16 at 09:45