Finding x in a comma separated list

# Thread: Finding x in a comma separated list

1. Senior Member
Join Date
Dec 1969
Posts
137

## Finding x in a comma separated list

An idiot question just for something different from me. How do I search for a particular string in a comma separated list of data. For example if I want to find "Prod1" in a list which contains: "Proda, Prodb, Prod1, Prodx".<BR><BR>I guess the answer is pretty simple but I&#039;ve been looking at the problem too long to find the logical answer. <BR><BR>Cheers<BR><BR>ab

2. Senior Member
Join Date
Dec 1969
Posts
701

## Something Like...

myString="Proda, Prodb, Prod1, Prodx"<BR>list=Split(myString, ",")<BR><BR>FOR i=0 to UBound(list)<BR><BR>IF list(i)="prod1" THEN<BR>response.write "FOUND IT"<BR>END IF<BR><BR>NEXT<BR><BR>does that work?

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## What do the commas matter?

&#060;%<BR>str = "Proda, Prodb, Prod1, Prodx"<BR>foundAt = InStr( str, "Prod1" )<BR>If foundAt &#062; 0 Then Response.Write "found at " & foundAt<BR>%&#062;<BR><BR>Now, the problem with this is you can get false matches.<BR>&#060;%<BR>str = "Proda, Prodb, Prod1000, Prodx"<BR>foundAt = InStr( str, "Prod1" )<BR>If foundAt &#062; 0 Then Response.Write "found at " & foundAt<BR>%&#062;<BR><BR>Oops...that&#039;s going to find Prod1 because it matches the first part of Prod1000.<BR><BR>There are lots of ways around this.<BR><BR>Perhaps the fastest way is to construct your "list" string more carefully.<BR>(a) No spaces.<BR>(b) Add a comma to the front and back of the string.<BR>(c) Search for your term with a comma added to front and back of it.<BR><BR>So:<BR>&#060;%<BR>str = ",Proda,Prodb,Prod1000,Prodx,"<BR>foundAt = InStr( str, ",Prod1," )<BR>If foundAt &#062; 0 Then Response.Write "found at " & foundAt<BR>%&#062;<BR><BR>Presto. No false match of Prod1 in Prod1000, because of the need to match the word boundaries.<BR><BR>You can do this same thing with a regular expression, specifying you want a word-boundary-only search, if you feel ready to delve into regular expressions. See the VBS docs for the RegExp object.<BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
137

## RE: What do the commas matter?

As usual you were right, they don&#039;t really matter. I wasn&#039;t thinking about the problem in broad terms, but i&#039;ve now seen the light. Or should that be, i&#039;ve now seen the ASPFAQ&#039;s. <BR><BR>If anyone else comes across this post searching for an answer to the comma separated list problem, I strongly suggest a quick peruse of the following FAQ:<BR><BR>http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=153<BR><BR>Cheers to all for your input.<BR><BR>ab

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## No fair!

Making fun of me using an FAQ that I wrote, even.<BR><BR>Heh.<BR><BR>Okay, you got me. But I had no idea that you were heading in that direction. I thought you really did want to solve the "find it" problem.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•