Re: Best way to check is a string is in a list?
- From: "KH" <KH@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 20 Jun 2005 13:23:05 -0700
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
> > >
> > >
> >
> >
>
>
>
.
- Follow-Ups:
- Re: Best way to check is a string is in a list?
- From: Mythran
- Re: Best way to check is a string is in a list?
- References:
- Best way to check is a string is in a list?
- From: Gerrit Beuze
- Re: Best way to check is a string is in a list?
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Best way to check is a string is in a list?
- From: Gerrit Beuze
- Best way to check is a string is in a list?
- Prev by Date: Re: SizeOf for managed types
- Next by Date: Re: How To get range of Value with Regular expression
- Previous by thread: Re: Best way to check is a string is in a list?
- Next by thread: Re: Best way to check is a string is in a list?
- Index(es):
Relevant Pages
|