split function

1. Member
Join Date
Dec 1969
Posts
40

## split function

&#060;% <BR> Call splitstring("Amaran/Teguran&&&&Lapor kepada LJUBM&","&")<BR> function splitstring(string,divider)<BR> For counter1 = 1 To len(string)<BR> if Mid(string,counter1,1) &#060;&#062; divider Then<BR> counter2 = counter2 + 1<BR> else<BR> tempstring = CStr(Mid(string,counter1 - counter2,counter2))<BR> <BR> %&#062;<BR>&#060;% &#060;td width="30%" align="left" valign="top"&#062;&#060;font face="Verdana" color="blue" size="1"&#062;&#060;%Response.Write(tempstring)&#0 62;&#060;/font&#062;&#060;/td&#062;<BR> counter2 = 0<BR> end if<BR> next<BR> end function<BR> %&#062;<BR><BR>split it&#039;s ok...but when i want to show it , it&#039;ll be in different td...so please help me...i want it appear like this :<BR>Amaran/Teguran<BR>Lapor kepada LJUBM<BR>that&#039;all..please help me really urgent...<BR>

2. Senior Member
Join Date
Dec 1969
Posts
5,955

## If you mean all in one <TD>...

...but separated by line breaks, you could do just move the loop into the TD, like this:<BR><BR>&#060;td width="30%" align="left" valign="top"&#062;&#060;font face="Verdana" color="blue" size="1"&#062;&#060;%Call splitstring("Amaran/Teguran&&&&Lapor kepada LJUBM&","&")<BR><BR>function splitstring(string,divider)<BR>&nbsp;&nbsp;&nbsp;F or counter1 = 1 To len(string)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;if Mid(string,counter1,1) &#060;&#062; divider Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;counter2 = counter2 + 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;tempstring = CStr(Mid(string,counter1 - counter2,counter2))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write(tempstri ng & "<BR>")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;counter2 = 0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end if<BR>&nbsp;&nbsp;&nbsp;next<BR>end function<BR>%&#062;&#060;/font&#062;&#060;/td&#062;<BR><BR>But why don&#039;t you use the Split() function? See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctSplit.asp for more details.<BR><BR>Oliver.

3. Member
Join Date
Dec 1969
Posts
40

## RE: If you mean all in one <TD>...

yap i want it in one&#060;TD&#062;..<BR>i&#039;ve tried that i suggested to me...it have an error "something with that array...<BR>so please help me, if you haveany idea...

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

## WHY would you EVER do that?

There&#039;s a SPLIT function *BUILT IN* to VBScript!<BR><BR>USE IT!<BR><BR>&#060;%<BR>...<BR>parts = SPLIT("Amaran/Teguran&&&&Lapor kepada LJUBM&" , "&" )<BR>For i = 0 To UBound(parts)<BR> part = TRIM( parts(i) )<BR> If part &#060;&#062; "" Then<BR>%&#062;<BR> &#060;td width="30%" align="left" valign="top"&#062;<BR> &#060;font face="Verdana" color="blue" size="1"&#062;&#060;%=part%&#062;&#060;/font&#062;<BR> &#060;/td&#062;<BR>&#060;%<BR>Next<BR>%&#062;<BR><BR>Why do something the hard way when VBS makes it so easy???<BR><BR>

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

## Sorry...left out the end if...

&#060;% <BR>... <BR>parts = SPLIT("Amaran/Teguran&&&&Lapor kepada LJUBM&" , "&" ) <BR>For i = 0 To UBound(parts) <BR> part = TRIM( parts(i) ) <BR> If part &#060;&#062; "" Then <BR>%&#062; <BR> &#060;td width="30%" align="left" valign="top"&#062; <BR> &#060;font face="Verdana" color="blue" size="1"&#062;&#060;%=part%&#062;&#060;/font&#062; <BR> &#060;/td&#062; <BR>&#060;% <BR> End If<BR>Next <BR>%&#062; <BR>

6. Member
Join Date
Dec 1969
Posts
40

## RE: Sorry...left out the end if...

thanks foryour help...but it still appear like this...<BR>1)Amaran/Teguran 2) 3) 4) 5) 6)Lapor kepada LJUBM 7)<BR><BR>i hope it can be like this<BR>1)Amaran/Teguran<BR>2)Lapor kepada LJUBM<BR><BR>if it can be like that....anyway thanks so much...

7. Senior Member
Join Date
Dec 1969
Posts
5,955

## Try this...

&#060;td width="30%" align="left" valign="top"&#062;&#060;font face="Verdana" color="blue" size="1"&#062;<BR>&#060;% <BR>...<BR>Dim count<BR>parts = split("Amaran/Teguran&&&&Lapor kepada LJUBM&" , "&" )<BR>count = 0<BR>For i = 0 To UBound(parts) <BR>&nbsp;&nbsp;&nbsp;&nbsp;part = TRIM( parts(i) ) <BR>&nbsp;&nbsp;&nbsp;&nbsp;If part &#060;&#062; "" Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;count = count + 1<BR>%&#062;&#060;%=count%&#062;) &#060;%=part%&#062;<BR>&#060;% <BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>Next<BR>%&#062;<BR>&#060;/font&#062;&#060;/td&#062;<BR><BR>P.S.: Bill, you&#039;re right of course. Using split() is much better.

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

## RE: Try this...

Help me to understand this:<BR><BR>******* Oliver&#039;s code *********<BR>&#060;% <BR>...<BR>Dim count<BR>parts = split("Amaran/Teguran&&&&Lapor kepada LJUBM&" , "&" )<BR>count = 0<BR>For i = 0 To UBound(parts) <BR> part = TRIM( parts(i) ) <BR> If part &#060;&#062; "" Then<BR> count = count + 1<BR>%&#062;&#060;%=count%&#062;) &#060;%=part%&#062;<BR>&#060;% <BR> End If<BR>Next<BR>%&#062;<BR>************************* ******<BR><BR>*********** My code ************<BR>&#060;% <BR>... <BR>parts = SPLIT("Amaran/Teguran&&&&Lapor kepada LJUBM&" , "&" ) <BR>For i = 0 To UBound(parts) <BR> part = TRIM( parts(i) ) <BR> If part &#060;&#062; "" Then <BR>%&#062; <BR> &#060;td width="30%" align="left" valign="top"&#062; <BR> &#060;font face="Verdana" color="blue" size="1"&#062;&#060;%=part%&#062;&#060;/font&#062; <BR> &#060;/td&#062; <BR>&#060;% <BR> End If <BR>Next <BR>%&#062; <BR>*****************<BR><BR>Okay, Oliver: Except for adding that COUNT in there--which so far as I could tell he didn&#039;t ask for, but never mind--how is your code different than mine????<BR><BR>

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

## Then you can't copy and paste...

...code properly.<BR><BR>I just tried it in my own page. <BR><BR>******* SplitDemo.asp *******<BR>&#060;HTML&#062;&#060;BODY&#062;<BR>&#0 60;TABLE Border=3 CellPadding=8&#062;<BR>&#060;TR&#062;<BR>&#060;% <BR>parts = SPLIT("Amaran/Teguran&&&&Lapor kepada LJUBM&" , "&" ) <BR>For i = 0 To UBound(parts) <BR> part = TRIM( parts(i) ) <BR> If part &#060;&#062; "" Then <BR>%&#062; <BR> &#060;td width="30%" align="left" valign="top"&#062; <BR> &#060;font face="Verdana" color="blue" size="1"&#062;&#060;%=part%&#062;&#060;/font&#062; <BR> &#060;/td&#062; <BR>&#060;% <BR> End If <BR>Next <BR>%&#062; <BR>&#060;/TR&#062;<BR>&#060;/TABLE&#062;<BR>&#060;/BODY&#062;&#060;/FORM&#062;<BR>***********************<BR><BR>And this is what I got for results when I VIEW The HTML source in the browser:<BR><BR>&#060;HTML&#062;&#060;BODY&#062;<B R>&#060;TABLE Border=3 CellPadding=8&#062;<BR>&#060;TR&#062;<BR> <BR> &#060;td width="30%" align="left" valign="top"&#062; <BR> &#060;font face="Verdana" color="blue" size="1"&#062;Amaran/Teguran&#060;/font&#062; <BR> &#060;/td&#062; <BR> <BR> &#060;td width="30%" align="left" valign="top"&#062; <BR> &#060;font face="Verdana" color="blue" size="1"&#062;Lapor kepada LJUBM&#060;/font&#062; <BR> &#060;/td&#062; <BR> <BR>&#060;/TR&#062;<BR>&#060;/TABLE&#062;<BR>&#060;/BODY&#062;&#060;/FORM&#062;<BR><BR>Yes, *exactly* the two cells. ONLY.<BR><BR><BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
5,955

## Pickachu wanted the information...

...to appear in separate lines and not all next to each other. Also, he didn&#039;t want to list or count empty entries. Hence, I took the &#060;td&#062; out of the loop and add a count which is only increased when an entry is found that&#039;s not empty.<BR><BR>I&#039;m not saying your code doesn&#039;t work, but Pickachu seems to have problems amending your code to make it work for what he wants. I just helped him with that tiny bit. I&#039;m not trying to pick a fight or anything.<BR><BR>Oliver.

#### Posting Permissions

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