473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 3819
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them......
3
by: Paul | last post by:
Hi all. Can someone provide some help on the following as there seems to be many different methods of acheiving the same outcome. Basically I am trying to develop a web service which will...
3
by: Bob | last post by:
It's not especially important, but I always like to know the best way of doing things for when I encounter a case where performance becomes a factor... say I have a string array and I want to get a...
3
by: Rich | last post by:
The procedure below checks if a character entered into a cell of a datagridview is contained in a string array of valid characters for this particular cell. It seems kludgy. I am asking what the...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
5
by: Andrew Meador | last post by:
I have a form (Change Card List by Status) with a check box (cboNOT) and a list box (lstStatus). There is an Open Report button that opens a report (Report - Change Card List) which uses a query...
16
by: skanemupp | last post by:
which is the best way to check if a string is an number or a char? could the 2nd example be very expensive timewise if i have to check a lot of strings? this value = raw_input() try:...
6
by: dudeja.rajat | last post by:
Hi, How to check if something is a list or a dictionary or just a string? Eg: for item in self.__libVerDict.itervalues(): self.cbAnalysisLibVersion(END, item) where __libVerDict is a...
6
by: tig2810 | last post by:
Hi I was wondering if someone could let me know the best/correct way of doing this. I have a shortened example of code below: In the beginning of the class I have a List of string containing...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.