Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wanted: .Net collection that stores a bunch of case insensitive strings fast and efficient

I'm looking for a simple collection that will store a bunch of strings in a case insensitive way. I need at least a Contains() and Remove() method to see if a certain string is present and to remove that string.

I've tried List<string> but that one is case sensitive. I need could use a case insensitive Dictionary<TKey, T>, but that "feels" like a waste of space. Doing a ToLower() on each string is a waste of performance.

Does anyone know what kind .Net collection I should use?

like image 334
Kees C. Bakker Avatar asked Jan 14 '11 19:01

Kees C. Bakker


People also ask

Does .NET contain case sensitive?

The string. Contains() method in C# is case sensitive. And there is not StringComparison parameter available similar to Equals() method, which helps to compare case insensitive.

What is case insensitive in C#?

Case insensitive containsIndexOf() finds the first occurrence of a particular string inside another string. The comparison type is determined by the StringComparison parameter, which we pass as the second parameter. String. IndexOf() returns the position of the first occurrence of a substring inside a string.

What is OrdinalIgnoreCase?

Remarks. 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.


2 Answers

You should use a new HashSet<string>(StringComparer.OrdinalIgnoreCase).
Note that this is an unordered set.

like image 88
SLaks Avatar answered Oct 07 '22 05:10

SLaks


You could use a StringDictionary.

like image 33
Reddog Avatar answered Oct 07 '22 07:10

Reddog