467,925 Members | 2,013 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,925 developers. It's quick & easy.

Case-insensitive List<string>

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
  • viewed: 22514
Share:
4 Replies
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
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
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
"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.

Similar topics

5 posts views Thread by Danny Anderson | last post: by
37 posts views Thread by Zombie | last post: by
17 posts views Thread by Karl Ebener | last post: by
11 posts views Thread by Martin Jørgensen | last post: by
6 posts views Thread by arnuld | last post: by
42 posts views Thread by barcaroller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.