By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

Case-insensitive List<string>

P: n/a
Hi,

Is it possible to create a case-insensitive List<stringcollection?

E.g.

List<stringMyList = 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,
stringcollections e.g.

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

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

Any assistance gratefully received.

Mark
Sep 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:Og****************@TK2MSFTNGP05.phx.gbl...
Hi,

Is it possible to create a case-insensitive List<stringcollection?

E.g.

List<stringMyList = 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,
stringcollections e.g.

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

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

Any assistance gratefully received.

Mark

Sep 24 '06 #2

P: n/a
Mark Rae wrote:
Hi,

Is it possible to create a case-insensitive List<stringcollection?

E.g.

List<stringMyList = 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,
stringcollections e.g.

Dictionary<string, stringMyDictionary = 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<strings = new List<string>();
bool contains = null != s.Find(delegate(string str)
{
return str.ToLower().Equals("MyString".ToLower());
});
HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Sep 24 '06 #3

P: n/a
Hi,

Performing case-insensitive comparisons using ToLower/ToUpper are very
expensive, a much more efficient means is to use String.Compare. For some
non-English languages the ToLower solution is also not accurate/correct. See
my post on case-insensitive string comparisons:
http://dotnetjunkies.com/WebLog/chri.../07/21393.aspx

--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor
"Andreas Mueller" <me@privacy.netwrote in message
news:4n************@individual.net...
Mark Rae wrote:
>Hi,

Is it possible to create a case-insensitive List<stringcollection?

E.g.

List<stringMyList = 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,
stringcollections e.g.

Dictionary<string, stringMyDictionary = 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<strings = new List<string>();
bool contains = null != s.Find(delegate(string str)
{
return str.ToLower().Equals("MyString".ToLower());
});
HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm

Sep 24 '06 #4

P: n/a
"Chris Taylor" <ch*************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Performing case-insensitive comparisons using ToLower/ToUpper are very
expensive, a much more efficient means is to use String.Compare. For some
non-English languages the ToLower solution is also not accurate/correct.
See my post on case-insensitive string comparisons:
http://dotnetjunkies.com/WebLog/chri.../07/21393.aspx
Thanks everyone.
Sep 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.