Re: Search Array
- From: "Jonathan Wood" <jwood@xxxxxxxxxxxxxxxx>
- Date: Tue, 20 Jan 2009 09:45:42 -0700
Yeah, I understand your point. Thanks.
Jonathan
"Pavel Minaev" <int19h@xxxxxxxxx> wrote in message news:e62f3c97-2369-4028-bb2e-46bef0a59f45@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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...
.
- References:
- Search Array
- From: Jonathan Wood
- Re: Search Array
- From: Stefan L
- Re: Search Array
- From: Jonathan Wood
- Re: Search Array
- From: Pavel Minaev
- Search Array
- Prev by Date: Re: Search Array
- Next by Date: Re: Search Array
- Previous by thread: Re: Search Array
- Next by thread: MSI Files
- Index(es):
Relevant Pages
|