473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
4 23904
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Danny Anderson | last post by:
Hola! I am working on a program where I am including a library that came with my numerical methods textbook. The "util.h" simply includes a large number of files. I had to change the util.h...
12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
17
by: Karl Ebener | last post by:
Hi! I asked a similar question before but then changed everything to using char-Arrays instead of the string class, but I would rather not do this again. So, does anyone know of a...
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
12
by: ibloom | last post by:
I've been trying for a couple days to build a program using pyObjC on a mac, I'm stuck on this error: <string>: inconsistent use of tabs and spaces in indentation Traceback (most recent call...
4
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.