How do I check if a given string is contained within either a string
array or a delimited list of strings?

eg:
strLevel = "red"

' i want to see if strLevel is one of the following, using either an array:
arrLevels = Array("red","yellow","blue")

' or a delimited list:
strLevels = "red,yellow,blue"

I just want to return a simple true/false or 1/0.

Cheers
Ben

Re: string comparison to string array by dlbjr

dlbjr
Wed Jul 14 21:50:49 CDT 2004

Function IsPresent(strItem,List)
IsPresent = False
If IsArray(List) Then
List = Join(List,",")
End If
If Len(strItem) > 0 And Len(List) > 0 Then
If InStr(1,List,strItem,1) > 0 Then
IsPresent = True
End If
End If
End Function

'Example 1
arrLevels = Array("red","yellow","blue")
strItem = "red"
msgbox IsPresent(strItem,arrLevels)

'Example 2
arrLevels = "red,yellow,blue"
strItem = "red"
msgbox IsPresent(strItem,arrLevels)

dlbjr
Pleading sagacious indoctrination!



Re: string comparison to string array by Clas

Clas
Fri Jul 16 10:49:23 CDT 2004

ben h <none@noone.com.invalid> wrote in news:O0Q7T5gaEHA.2816
@TK2MSFTNGP11.phx.gbl:

> How do I check if a given string is contained within either a string
> array or a delimited list of strings?

May the function IsArray(VarName) helps you here.

Regards
Clas

--
The light at the end of the tunnel,
was switch off due to high costs.

Re: string comparison to string array by Helge

Helge
Mon Jul 26 06:00:54 CDT 2004

On Wed, 14 Jul 2004 21:50:49 -0500, "dlbjr" <oops@iforgot.com> wrote:

> If InStr(1,List,strItem,1) > 0 Then

Should be changed to

If InStr(1, "," & List & ",", "," & strItem & ",", 1) > 0 Then

That way, you'll not run the risk of getting a false hit if the
searched string happens to appear as a substring in one of the list
entries.

I.e., if the list contains "Czechoslovakia", you wouldn't want a hit
if you searched for "Oslo".

If list items can contain commas, you must pick a different separator.

--
Helge Wunderlich
(Please remove the spam trap if you wish to send email)