checking for any value in an array

  • Thread starter Thread starter darrel
  • Start date Start date
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
 
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.
 
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.
 
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
 
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
 
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.
 
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
 
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...
 
Back
Top