checking for any value in an array

D

darrel

let's say I have a string that can be any number of items comma delimited:

2, 4, 6, 12, 345

I want to check against those values in an if/then statement:

if integer = any value in the string, then...

It seems the best way to do this would be to convert the string into an
array. Once I do that, is there a simple way to write out

if [my value] = [any item in the array collection] then...

or do I need to loop through each array item one by one looking for a match?

-Darrel
 
M

Mark Rae

or do I need to loop through each array item one by one looking for a
match?

Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.
 
S

Shuvro Mazumder [MSFT]

Yes, the only way is to convert the string to an integer array using a split
and search inside the array. If your array is very large, you might want to
consider sorting it and doing a binary search for the value to reduce your
search space.

Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".

But again, this method is a bit of hack, so I would recommend the first
method.

Hope this helps
--
- Shuvro
SDE, MSFT

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
 
D

darrel

Another non-recommended way (which nevertheless works in some cases) is to
take the original string and take out all spaces and add a "," at the
begining and end so that it looks like:
,num1,num2,num3,num4,
And if you want to search for your number "numX", you can so a substring
search for ",numX,".

But again, this method is a bit of hack, so I would recommend the first
method.

Hmm...it does seem like a hack, but, at the same time, seems more efficient.
It's already a text string to begin with, and with this, I'm only searching
once, instead of looping through it all.

Not that I don't need to learn arrays, of course. ;o)

-Darrel
 
D

darrel

or do I need to loop through each array item one by one looking for a
Use an ArrayList instead of an Array - ArrayList objects have a Contains()
method which tells you if a value is contained within the ArrayList.

Thanks! I will look into ArrayLists!

-Darrel
 
S

sam

Not to beat this to death, but if you plan to do this a lot you should
put them as keys into a hashtable with the value set to a dummy '1' or
something. Then you can do hashtable.ContainsKey(value).

This is done pretty much in constant time, and isn't linear to length
like the Contains() method.
 
D

darrel

Not to beat this to death, but if you plan to do this a lot you should
put them as keys into a hashtable with the value set to a dummy '1' or
something. Then you can do hashtable.ContainsKey(value).

This is done pretty much in constant time, and isn't linear to length
like the Contains() method.

I've never used hashtables. Will have to check that out!

-Darrel
 
M

Mark Rae

Thanks! I will look into ArrayLists!

Do yourself a favour and check out the other members of the
System.Collections namespace e.g. SortedList, Hashtable etc - massive
improvements over the old Dictionary, Collection objects etc...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top