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

  • Thread starter Thread starter Gerrit Beuze
  • Start date Start date
G

Gerrit Beuze

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
 
Gerrit Beuze said:
Any suggestions on the data structure to use?
Does the 1.1 framework come with sorted (case insentive) stringlists ?

Coming from Delphi the FCL objects are very specialized and confusing at first compared to
Delphi's Swiss Army Knife TStrings.

Take a look in System.Collections.Specialized, but probably the best depends on the size of your
list, etc.. But I would just store them all lower case (or uppercase) and perform the search using
the same.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Gerrit,

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.
 
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]
- (e-mail address removed)

Gerrit Beuze said:
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
 
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 said:
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]
- (e-mail address removed)

Gerrit Beuze said:
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
 
KH said:
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.


That is how I would do it :)

Mythran
 
Back
Top