Case insensitive generic list search ?

H

hdjim

I have a generic list of strings List<string> which I use the Contains
method to search but Contains does not provide a second parameter to
ignore case. What is the best way to do a search on a list ignoring
the case?

Example
List<string> lClasses = GetAllClasses();
String myClass = “myClass”;
if (lClasses.Contains(myClass))
do something

If myClass is not the correct case it’s not found. Should I use an
Array? Force everything to Upper?

TIA
hd
 
J

Jeff Johnson

hdjim said:
I have a generic list of strings List<string> which I use the Contains
method to search but Contains does not provide a second parameter to
ignore case. What is the best way to do a search on a list ignoring
the case?
Example
List<string> lClasses = GetAllClasses();
String myClass = “myClass”;
if (lClasses.Contains(myClass))
do something
If myClass is not the correct case it’s not found. Should I use an
Array? Force everything to Upper?

If the case of the strings is not important then forcing to upper will work.

If case is important but the order of the strings in the list is not
important then you could sort the list and then do a BinarySearch passing
your own Comparer<string> derivative which ignores case.

If all of these things are important, looks like you'll just have to forego
Contains() and use a loop.
 
B

Ben Voigt [C++ MVP]

hdjim said:
I have a generic list of strings List<string> which I use the Contains
method to search but Contains does not provide a second parameter to
ignore case. What is the best way to do a search on a list ignoring
the case?

Example
List<string> lClasses = GetAllClasses();
String myClass = “myClass”;
if (lClasses.Contains(myClass))

found = lClasses.FindIndex(delegate(string element) { return 0 ==
string.Compare(myClass, element, StringComparison.OrdinalIgnoreCase); });
if (found >= 0) ...
 
H

hdjim

If the case of the strings is not important then forcing to upper will work.

If case is important but the order of the strings in the list is not
important then you could sort the list and then do a BinarySearch passing
your own Comparer<string> derivative which ignores case.

If all of these things are important, looks like you'll just have to forego
Contains() and use a loop.

OK. Thanks.
 
J

Jeff Johnson

found = lClasses.FindIndex(delegate(string element) { return 0 ==
string.Compare(myClass, element, StringComparison.OrdinalIgnoreCase); });
if (found >= 0) ...

I think I need amend all my reponses to include "unless there's a way to do
it with LINQ."
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

Well, with LINQ, it's pretty much the same as Ben's suggestion:

if (lClasses.Where(c => string.Compare(myClass, c,
StringComparison.OrdinalIgnoreCase) == 0).Any())
{
// Do your work here.
}
 
J

Jeff Johnson

Well, with LINQ, it's pretty much the same as Ben's suggestion:

if (lClasses.Where(c => string.Compare(myClass, c,
StringComparison.OrdinalIgnoreCase) == 0).Any())
{
// Do your work here.
}

Oh, yeah. He just used an extension method. I tend to think of extension
methods and LINQ as the same thing. Probably because I know next to nothing
about either of them and they both came out at the same time.
 
B

Ben Voigt [C++ MVP]

Jeff said:
Oh, yeah. He just used an extension method. I tend to think of
extension methods and LINQ as the same thing. Probably because I know
next to nothing about either of them and they both came out at the
same time.

And what extension method would that be, exactly? The code I posted is
valid in C# 2.x just like List<>.Contains
 
J

Jeff Johnson

And what extension method would that be, exactly? The code I posted is
valid in C# 2.x just like List<>.Contains

Oh, wrong again, huh? So, let's see if I got this right: you used an
anonymous delegate, yes? I thought those were new to C# 3.0. Well, let me
RTFM....

And there we go. C# 2.0 has them. I guess if you don't use them, you don't
tend to know they exist.

I'm going to pretend this thread never existed....
 
J

Jeff Johnson

And what extension method would that be, exactly? The code I posted is
valid in C# 2.x just like List<>.Contains

Apparently I've been under the impression that "extension methods" and
"methods which take Predicate arguments" were the same thing. I see now that
they are not.
 
B

Ben Voigt [C++ MVP]

Jeff Johnson said:
Oh, wrong again, huh? So, let's see if I got this right: you used an
anonymous delegate, yes? I thought those were new to C# 3.0. Well, let me
RTFM....

And there we go. C# 2.0 has them. I guess if you don't use them, you don't
tend to know they exist.

I'm going to pretend this thread never existed....

Hey, you came here wanting to learn something, right? Sounds like you got
your wish.
 
B

Ben Voigt [C++ MVP]

Jeff Johnson said:
Apparently I've been under the impression that "extension methods" and
"methods which take Predicate arguments" were the same thing. I see now
that they are not.

Extension delegates are the "static method with an explicit this argument"
where you can use the "var.method()" syntax to implicitly pass the this
argument, but the function receives it explicitly. This way the method
doesn't actually have to be a member of the object you call it on.

Although extension methods don't have anything to do with delegates, there
is a connection, kind of:

The difference between extension methods and member methods is rather like
the difference between open and closed delegates. Just like you can call an
extension method directly passing the target object as an argument, you can
use an open delegate to call a member method passing the target object as an
argument.
 
N

Nick Olsen

Here is an example of how to write a case insensitive Contains extension method on a list of strings.

http://nickstips.wordpress.com/2010/08/24/c-ignore-case-on-list-contains-method/

Hope it helps!

Nick
If the case of the strings is not important then forcing to upper will work.

If case is important but the order of the strings in the list is not
important then you could sort the list and then do a BinarySearch passing
your own Comparer<string> derivative which ignores case.

If all of these things are important, looks like you'll just have to forego
Contains() and use a loop.
found = lClasses.FindIndex(delegate(string element) { return 0 ==
string.Compare(myClass, element, StringComparison.OrdinalIgnoreCase); });
if (found >= 0) ...
On Thursday, December 11, 2008 1:48 PM Nicholas Paldino [.NET/C# MVP] wrote:
Jeff,

Well, with LINQ, it's pretty much the same as Ben's suggestion:

if (lClasses.Where(c => string.Compare(myClass, c,
StringComparison.OrdinalIgnoreCase) == 0).Any())
{
// Do your work here.
}

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

news:[email protected]...
On Friday, December 12, 2008 4:07 PM Ben Voigt [C++ MVP] wrote:
Hey, you came here wanting to learn something, right? Sounds like you got
your wish.
On Friday, December 12, 2008 4:10 PM Ben Voigt [C++ MVP] wrote:

Extension delegates are the "static method with an explicit this argument"
where you can use the "var.method()" syntax to implicitly pass the this
argument, but the function receives it explicitly. This way the method
doesn't actually have to be a member of the object you call it on.

Although extension methods don't have anything to do with delegates, there
is a connection, kind of:

The difference between extension methods and member methods is rather like
the difference between open and closed delegates. Just like you can call an
extension method directly passing the target object as an argument, you can
use an open delegate to call a member method passing the target object as an
argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top