473,765 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Case-insensitive List<string>

Hi,

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

E.g.

List<stringMyLi st = new List<string>;
MyList.Add("MyS tring");

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<stri ng,
stringcollectio ns e.g.

Dictionary<stri ng, stringMyDiction ary = new Dictionary<stri ng,
string>(StringC omparer.Current CultureIgnoreCa se);

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

Any assistance gratefully received.

Mark
Sep 24 '06 #1
4 23966
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.co m

"Mark Rae" <ma**@markNOSPA Mrae.comwrote in message
news:Og******** ********@TK2MSF TNGP05.phx.gbl. ..
Hi,

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

E.g.

List<stringMyLi st = new List<string>;
MyList.Add("MyS tring");

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<stri ng,
stringcollectio ns e.g.

Dictionary<stri ng, stringMyDiction ary = new Dictionary<stri ng,
string>(StringC omparer.Current CultureIgnoreCa se);

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<stringcoll ection?

E.g.

List<stringMyLi st = new List<string>;
MyList.Add("MyS tring");

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<stri ng,
stringcollectio ns e.g.

Dictionary<stri ng, stringMyDiction ary = new Dictionary<stri ng,
string>(StringC omparer.Current CultureIgnoreCa se);

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().E quals("MyString ".ToLower() );
});
HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gm xNOSPAm.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.net wrote in message
news:4n******** ****@individual .net...
Mark Rae wrote:
>Hi,

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

E.g.

List<stringMyL ist = new List<string>;
MyList.Add("My String");

So that:

MyList.Contain s("mystring")
MyList.Contain s("MyString")
MyList.Contain s("MYSTRING")

all return true.

Google shows plenty of examples of how to do this with Dictionary<stri ng,
stringcollecti ons e.g.

Dictionary<str ing, stringMyDiction ary = new Dictionary<stri ng,
string>(String Comparer.Curren tCultureIgnoreC ase);

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().E quals("MyString ".ToLower() );
});
HTH,
Andy
--
You can email me directly by removing the NOSPAm below
xm**********@gm xNOSPAm.netNOSPAm

Sep 24 '06 #4
"Chris Taylor" <ch************ *@hotmail.comwr ote in message
news:%2******** ********@TK2MSF TNGP02.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
6096
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 slightly to adjust path names and also take into account I am working with a case-sensitive OS. My program is below. The sticky point is that adding (#include "util.h") seems to negate the (#include <string>) statement somehow. How can I get...
12
5064
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 it to clear the temp vector for each file. ********************* code *******************
37
7361
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 pointer which cannot be used with strlwr() as it does the conversion inplace. So, I use the following logic of copying the contents to a dynamically allocated char* array and then doing the conversion: -----------------------------
17
3855
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 string-Class similar to the STL-<string> that supports null-bytes? I tried with standard <string> but this definitely does not support
11
8795
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
6
5716
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
12
7554
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 last): File "setup.py", line 59, in ? setup(**setup_options) File "/Library/Frameworks/Python.framework/Versions/2.4//lib/ python2.4/distutils/core.py", line 149, in setup dist.run_commands()
4
8252
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 am trying to fix things with <string>.encode he Company�s ticker Trying for an encode:
42
4543
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 of string, in one line. I could
9
3814
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 representation of vector<string>? Will the vector contain the string objects or will it contain pointers/references to the string objects? The reason I ask is that it is not clear to me how the v.reserve() and &v operations would work for a vector<string>.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.