1

After reading this I'm still confused :

 string s1 = "hello";
 string s2 = "héllo";

The difference thing here is the accent/culture.

The result of the following code is False.

Console.WriteLine(s1.Equals(s2, StringComparison.InvariantCulture));

But Im using invariant culture , so it should treat é as e.( default is English ,no?)

it seems that I have to go all the way to use

String.Compare(String, String, CultureInfo, CompareOptions) 

like

  string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) //true

But , my current culture is he-IL so I have no CLUE why it is working.

so :

  • I can't understand when the CompareOptions didn't work although I used StringComparison.InvariantCulture ( and please don't reference me to the msdn page , cause I've already read it and I don't fully understand their explanation )

  • In simple words , when should I use each overload ?

  • Doesn't nonspacing combining characters is a culture thing ?

The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 9
    `InvariantCulture` is basically a non-changing English useful for stuff like serialization or config files, but not for comparisons. – user7116 Jan 11 '13 at 19:26
  • @sixlettervariables thanks. but if you think your answer made me understand those question i asked, you're wrong....:-)...(and what do you mean by _non-changing English_) – Royi Namir Jan 11 '13 at 19:29
  • 1
    Whether `é` and `e` are the same or not is very Culture dependent. There is no reason for InvariantCulture to support a specific view here. – H H Jan 11 '13 at 19:29
  • That's not an answer, it's a comment and it does offer salient information relating to your question, so no need to be mean about it. – Pete Jan 11 '13 at 19:31
  • 3
    This http://stackoverflow.com/questions/2423377/what-is-the-invariant-culture and this http://stackoverflow.com/questions/492799/difference-between-invariantculture-and-ordinal-string-comparision are almost duplicates and combined probably answer your question. – Alexei Levenkov Jan 11 '13 at 19:32
  • @Pete do you see the smily at the end ? – Royi Namir Jan 11 '13 at 19:32
  • 2
    @Pete: I took no offense, being blunt is perfect in this case. I'm working on an actual answer. – user7116 Jan 11 '13 at 19:33

1 Answers1

3

Your confusion with InvariantCulture is pretty common. The best use for this is when you are persisting data to and from a file and do not care about a given culture's oddities (such as , as a decimal separator or spelling flavor with a 'u').

It has limited use in comparisons, especially when you need culture specific behavior. It may not seem obvious on face value, but comparing e with an acute accent to be the same as e without one...well that is situation dependent.

Aha! Situation dependent you say.

Looks like a job for a culture specific overload. Anytime you know which culture you are using, you should pass that culture along.

In this case, you would like .Net to ignore diacritical marks (p. ex. accent aigu), hence you should also use an overload which accepts CompareOptions (specifically as you noted CompareOptions.IgnoreNonSpace).

user7116
  • 63,008
  • 17
  • 141
  • 172