Splitting up a string

# Thread: Splitting up a string

1. Senior Member
Join Date
Dec 1969
Posts
191

## Splitting up a string

If I have a string of say, 1,12,24,16,4, how do I get the numbers out of it to work with them individually?<BR><BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Splitting up a string

str = "1,12,24,16,4"<BR>str = Split(str,",")<BR><BR>Str(0) ===&#062; 1<BR>Str(1) ===&#062; 12<BR>etc.<BR>

3. Senior Member
Join Date
Dec 1969
Posts
191

## RE: Splitting up a string

Let&#039;s say I don&#039;t know how many numbers are in the string to pull...can I do a For Each number In (something)?<BR><BR>Thanks!

4. Senior Member
Join Date
Dec 1969
Posts
2,049

## RE: Absolutely

&nbsp;<BR>For Each i In srt<BR><BR>Next<BR><BR>OR<BR><BR>For i = 0 To UBound(str)<BR><BR>Next

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

## Caution...

There&#039;s a significant difference in those two methodologies.<BR><BR>To illustrate:<BR><BR>&#060;%<BR>str = "1,2,3,6,9,11,2"<BR>arr = Split( str, "," )<BR><BR>For each arrElem In arr<BR> Response.Write arrElem & "&#060;br/&#062;"<BR>Next<BR><BR>For arrIndex = 0 To UBound(arr)<BR> arrElem = arr(arrIndex)<BR> Response.Write arrElem & "&#060;br/&#062;"<BR>Next<BR>%&#062;<BR><BR>The two loops will end up displaying the same results, but note the "extra step" needed in the second case. The FOR loop control variable in FOR EACH actually contains the *values* of successive elements. In the standard FOR = TO loop, the control variable is the index *into* the array, not the value directly.<BR><BR>Why would you ever use the "longer" form with an index? Because you might have need of the index number. Because you might want to be able to change the value in the array. Neither of which you can do with the FOR EACH form.<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
•