Case-insensitive List<string>

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

Is it possible to create a case-insensitive List<string> collection?

E.g.

List<string> MyList = new List<string>;
MyList.Add("MyString");

So that:

MyList.Contains("mystring")
MyList.Contains("MyString")
MyList.Contains("MYSTRING")

all return true.

Google shows plenty of examples of how to do this with Dictionary<string,
string> collections e.g.

Dictionary<string, string> MyDictionary = new Dictionary<string,
string>(StringComparer.CurrentCultureIgnoreCase);

but I can't find an equivalent for List<string>...

Any assistance gratefully received.

Mark
 
Mark,

No, the List does not support any other comparison for Contains. You
will have to use the dictionary method to determine if you have a
case-insensitive string in your set.

Hope this helps.
 
Mark said:
Hi,

Is it possible to create a case-insensitive List<string> collection?

E.g.

List<string> MyList = new List<string>;
MyList.Add("MyString");

So that:

MyList.Contains("mystring")
MyList.Contains("MyString")
MyList.Contains("MYSTRING")

all return true.

Google shows plenty of examples of how to do this with Dictionary<string,
string> collections e.g.

Dictionary<string, string> MyDictionary = new Dictionary<string,
string>(StringComparer.CurrentCultureIgnoreCase);

but I can't find an equivalent for List<string>...

Any assistance gratefully received.

Mark
No, but you can get the information you need from a regular List<string>:

List<string> s = new List<string>();
bool contains = null != s.Find(delegate(string str)
{
return str.ToLower().Equals("MyString".ToLower());
});


HTH,
Andy
 
Back
Top