Tutorial :Problem comparing French character ÃŽ



Question:

When comparing "ÃŽle" and "Ile", C# does not consider these to be to be the same.

    string.Equals("ÃŽle", "Ile", StringComparison.InvariantCultureIgnoreCase)  

For all other accented characters I have come across the comparison works fine.

Is there another comparison function I should use?


Solution:1

You are specifying to compare the strings using the Invariant culture's comparison rules. Evidently, in the invariant culture, the two strings are not considered equal.

You can compare them in a culture-specific manner using String.Compare and providing the culture for which you want to compare the strings:

if(String.Compare("ÃŽle", "Ile", new CultureInfo("fr-FR"), CompareOptions.None)==0)  

Please note that in the french culture, those strings are also considered different. I included the example to show, that it is the culture that defines the sort rules. You might be able to find a culture that fits your requirements, or build a custom one with the needed compare rules, but that it probably not what you want.

For a good example of normalizing the string so there are no accents, have a look at this question. After normalizing the string, you would be able to compare them and consider them equal. This would probably be the easiest way to implement your requirement.

Edit

It is not just the I character that has this behaviour in the InvariantCulture, this statement also returns false:

String.Equals("Ilê", "Ile", StringComparison.InvariantCultureIgnoreCase)  

The framework does the right thing - those characters are in fact different (has different meaning) in most cultures, and therefore they should not be considered the same.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »