Re: Search Array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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...

.



Relevant Pages

  • System.String Compare vs. IndexOf
    ... I need to find all occurrences of an input word in very large array of ... strings. ... The second approach (IndexOf) is much ... And the second reason is array boundary checkings are not performed. ...
    (microsoft.public.dotnet.framework)
  • Re: Search Array
    ... IndexOf for any other collection) is that IndexOf is generic - it is ... the same for numbers, strings, and pretty much anything. ... only strings even have the notion of case-sensitivity, ... for whatever type you have an array of. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Program slow. Garbage Collection
    ... but only if the searched-for strings are present ... "IndexOf" internally. ... The first priority should be readable, easy-to-understand, easy-to-maintain code. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Contains for string?
    ... little bit strange routine when I would make this, ... standard in dotnet as regex, indexof, etc. ... > I think the theoretically fastest implementation would be an adapted ... > of three strings in the text) there may be comparisons done that don't ...
    (microsoft.public.dotnet.languages.vb)
  • Re: C# String Comparison, IndexOf and Related
    ... "BILL" wrote in message ... I'm speaking of larger strings to search as well ~50K-500K. ... > * ToUpper/ToLower and IndexOf would be quite slow, ... > * Any unsafe code, Boyer-Moore using pointers or inline assembly (if ...
    (microsoft.public.dotnet.languages.csharp)