Re: Search Array
- From: Pavel Minaev <int19h@xxxxxxxxx>
- Date: Mon, 19 Jan 2009 16:33:00 -0800 (PST)
On Jan 19, 3:53 pm, "Jonathan Wood" <jw...@xxxxxxxxxxxxxxxx> wrote:
That's the type of things I was looking for. Unfortunately, it does not meet
my requirements as it is case-sensitive.
The simple reason why there's no case-insensitive Array.IndexOf (nor
IndexOf for any other collection) is that IndexOf is generic - it is
the same for numbers, strings, and pretty much anything. Of those,
only strings even have the notion of case-sensitivity, so a generic
method has no way to account for that.
Think of it that way. IndexOf uses the "generic equality" comparison
for whatever type you have an array of. What you want is a special
kind of string comparison. So, you need to provide your own comparison
predicate. And yes, you can do that with Find (or rather, FindIndex,
if what you want to get is the index of the string in the collection):
int indexOfFoo = Array.FindIndex(strings, s => "foo".Equals(s,
StringComparison.CurrentCultureIgnoreCase));
Plain Find (which returns the value found) can be used in the same way
as well. By the way, note that it does "foo".Equals(s) rather than
s.Equals("foo") - this is probably obvious, but it's just in case the
array has some null elements...
.
- Follow-Ups:
- Re: Search Array
- From: Jonathan Wood
- Re: Search Array
- References:
- Search Array
- From: Jonathan Wood
- Re: Search Array
- From: Stefan L
- Re: Search Array
- From: Jonathan Wood
- Search Array
- Prev by Date: Re: Search Array
- Next by Date: most cost effective way to persist page level objects in server side in asp.net
- Previous by thread: Re: Search Array
- Next by thread: Re: Search Array
- Index(es):
Relevant Pages
|