Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Version of StringComparer to use

If I want to have a case-insensitive string-keyed dictionary, which version of StringComparer should I use given these constraints:

  • The keys in the dictionary come from either C# code or config files written in english locale only (either US, or UK)
  • The software is internationalized and will run in different locales

I normally use StringComparer.InvariantCultureIgnoreCase but wasn't sure if that is the correct case. Here is example code:

Dictionary< string, object> stuff = new Dictionary< string, object>(StringComparer.InvariantCultureIgnoreCase);
like image 514
Ted Elliott Avatar asked Oct 09 '08 17:10

Ted Elliott


People also ask

How do you use the various Stringcomparers?

You can use the StringComparer class to create a type-specific comparison to sort the elements in a generic collection. Classes such as Hashtable, Dictionary<TKey,TValue>, SortedList, and SortedList<TKey,TValue> use the StringComparer class for sorting purposes.

What is StringComparer OrdinalIgnoreCase?

The StringComparer returned by the OrdinalIgnoreCase property treats the characters in the strings to compare as if they were converted to uppercase using the conventions of the invariant culture, and then performs a simple byte comparison that is independent of language.

What is Stringcomparison InvariantCultureIgnoreCase C#?

InvariantCultureIgnoreCase. The StringComparer returned by the InvariantCultureIgnoreCase property compares strings in a linguistically relevant manner that ignores case, but it is not suitable for display in any particular culture.


3 Answers

There are three kinds of comparers:

  • Culture-aware
  • Culture invariant
  • Ordinal

Each comparer has a case-sensitive as well as a case-insensitive version.

An ordinal comparer uses ordinal values of characters. This is the fastest comparer, it should be used for internal purposes.

A culture-aware comparer considers aspects that are specific to the culture of the current thread. It knows the "Turkish i", "Spanish LL", etc. problems. It should be used for UI strings.

The culture invariant comparer is actually not defined and can produce unpredictable results, and thus should never be used at all.

References

  1. New Recommendations for Using Strings in Microsoft .NET 2.0
like image 164
Michael Damatov Avatar answered Sep 21 '22 05:09

Michael Damatov


This MSDN article covers everything you could possibly want to know in great depth, including the Turkish-I problem.

It's been a while since I read it, so I'm off to do so again. See you in an hour!

like image 30
Greg Beech Avatar answered Sep 20 '22 05:09

Greg Beech


The concept of "case insensitive" is a linguistic one, and so it doesn't make sense without a culture.

See this blog for more information.

That said if you are just talking about strings using the latin alphabet then you will probably get away with the InvariantCulture.

It is probably best to create the dictionary with StringComparer.CurrentCulture, though. This will allow "ß" to match "ss" in your dictionary under a German culture, for example.

like image 21
Oliver Hallam Avatar answered Sep 24 '22 05:09

Oliver Hallam