471,599 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

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

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
Nov 17 '05 #1
5 3711
"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in
news:ei**************@TK2MSFTNGP15.phx.gbl:
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/
Nov 17 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
news:ei**************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
news:ei**************@TK2MSFTNGP15.phx.gbl...
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


Nov 17 '05 #4
KH
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]
- mv*@spam.guard.caspershouse.com

"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
news:ei**************@TK2MSFTNGP15.phx.gbl...
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



Nov 17 '05 #5

"KH" <KH@discussions.microsoft.com> wrote in message
news:D3**********************************@microsof t.com...
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

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Steve | last post: by
5 posts views Thread by Andrew Meador | last post: by
16 posts views Thread by skanemupp | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.