Re: Best way to check is a string is in a list?

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



If you don't need/want the overhead of using a HashTable you could put all
the keywords into an array, sort the array, and use Array.BinarySearch().

You could pass a CaseInsensitiveComparer to BinarySearch(), or you could
store all the keywords in lower case and call ToLower() on the input string
(or use upper case if you'd prefer).

You might also look at using the hash code of your keywords as the key for
the HashTable, which would probably be faster than doing a case-insensitive
comparison on the keys.


"Gerrit Beuze" wrote:

> Yep, that should do it,
>
> Thanks,
>
> Gerrit Beuze
> ModelMaker Tools
>
> > The hashtable does support a case insensitive comparer. There is an
> > overload of the constructor which you can pass a new instance of a
> > CaseInsensitiveComparer and a CaseInsensitiveHashcodeProvider, and this will
> > give you what you want for the case-insensitive keys.
> >
> > Hope this helps.
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
> >
> > "Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
> > news:eiUiWCcdFHA.2984@xxxxxxxxxxxxxxxxxxxxxxx
> > > Hi all,
> > >
> > > Using C# 1.1:
> > > I need a fast way of determining whether a string is in a list of approx
> > > 150 keyword like strings.
> > > in two versions: one case-sensitive, the other one case-insenstive.
> > >
> > > I thought of loading the keywords in a Hastable as Keys with null values,
> > > and then use Hastable.ContainsKey(string)
> > > but Hashtable does not seem to support case-insentive keys.
> > >
> > > Linear search using a StringCollection is too slow for what I'm looking
> > > for.
> > >
> > > Any suggestions on the data structure to use?
> > > Does the 1.1 framework come with sorted (case insentive) stringlists ?
> > >
> > > Thanks in advance,
> > >
> > > Gerrit Beuze
> > > ModelMaker Tools
> > >
> > >
> >
> >
>
>
>
.



Relevant Pages

  • Re: String Replace Problem...
    ... HTH ... > # Open And Read The File With Keywords ... > onread = fid.read ... > for keys in keywords: ...
    (comp.lang.python)
  • Re: String Replace Problem...
    ... > # Open And Read The File With Keywords ... > onread = fid.read ... > for keys in keywords: ... > Andrea. ...
    (comp.lang.python)
  • Join Tables - Data Entry
    ... keywords that apply to various film titles. ... intermediate table called 'Occurrence' that contains foreign keys from ... But what if you don't have a lexicon of keywords already established ...
    (comp.databases.filemaker)
  • String Replace Problem...
    ... I would like to replace all the occurrencies of the "keywords" (beginning ... # Set Some Dummy Parameter Values ... for keys in keywords: ... onread = string.replace)) ...
    (comp.lang.python)
  • Re: Javascript code working with Firefox but not with IE... please help!
    ... I'm a javascript newbie, and I wrote a function who's purpose is to ... javascript error and the script doesn't do anything. ... It is more efficient to use an array literal: ... provided your keywords don't match markup rather than content. ...
    (comp.lang.javascript)