473,732 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Contai nsKey(string)
but Hashtable does not seem to support case-insentive keys.

Linear search using a StringCollectio n 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 3837
"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in
news:ei******** ******@TK2MSFTN GP15.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.Collecti ons.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/
"Programmin g 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
CaseInsensitive Comparer and a CaseInsensitive HashcodeProvide r, 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.co m

"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
news:ei******** ******@TK2MSFTN GP15.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.Contai nsKey(string)
but Hashtable does not seem to support case-insentive keys.

Linear search using a StringCollectio n 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
CaseInsensitive Comparer and a CaseInsensitive HashcodeProvide r, 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.co m

"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
news:ei******** ******@TK2MSFTN GP15.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.Contai nsKey(string)
but Hashtable does not seem to support case-insentive keys.

Linear search using a StringCollectio n 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.BinarySea rch().

You could pass a CaseInsensitive Comparer 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
CaseInsensitive Comparer and a CaseInsensitive HashcodeProvide r, 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.co m

"Gerrit Beuze" <gerrit[at]modelmaker[dot]demon[dot]nl> wrote in message
news:ei******** ******@TK2MSFTN GP15.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.Contai nsKey(string)
but Hashtable does not seem to support case-insentive keys.

Linear search using a StringCollectio n 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******** *************** ***********@mic rosoft.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.BinarySea rch().

You could pass a CaseInsensitive Comparer 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
3134
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... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
3
1622
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 spawn an exe file to run an import and then return a result back to the web service, this would also need the facility to spawn multiple copies of the application upto a maximum figure..............
3
1278
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 distinct list, that is, get a list without duplicates. In SQL this is easy because it's done for you. In code, what would be the best practice? Array.Sort(stringarray) followed by n comparisons collecting on the changes? Or perhaps adding each...
3
4312
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 best practice would be. I was thinking I could use an arrayList which has the "contains" property and do this: If not arr.Contains(s1) then --- don't continue but my list of codes might be about 50 char combinations. So I was thinking a...
9
7903
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 List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
7
3582
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 - FirstName - LastName - CompanyId (many-to-one )
5
6742
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 (SQL -Change Card List). What I want to do is have the form open the report where a filter is set to use the values from the check box AND the value selected from the list box to generate the report. What I can't figure out is how to use the...
16
14740
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: value = int(value)
6
3012
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 dictionary that holds values as strings or
6
2759
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 all records. I have added each encapsulated field to the list in the set property and I need to maintain the order in the list. Is this way best way to do this? Or should I be doing this another cleaner way. I guess I’m asking for best coding...
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.