Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cutting a string in 2 when a delimiter has been seen after x times?

Status
Not open for further replies.

FranckM

Programmer
May 8, 2002
76
0
0
CA
Is there a way to cut a string in 2 once a delimiter has been seen x amount of times?

This is what I'd be starting with:
string = (bob,june,sally,eric,joel)

This is what I would have at the end:
string1 = (bob,june)
string2 = (sally,eric,joel)

Thanks for taking the time to read and write ;)
 
there may be a far better way but you could:

1. split the string into an array
arrWords = Split(strText, ",")

2.get the length of the new array
Dim iLen
iLen = UBound(arrWords)

3. Divide it by 2
Const iTwo = 2
iLen = iLen / iTwo

4. Loop through array building one string then the other
For iCounter = 0 to UBound(arrWords)
Dim string1, string2
if( iCounter < iLen ) then
string1 = string1 + arrWords(iCounter)
else
string2 = string2 + arrWords(iCounter)
end if
Next
Response.write &quot;<li>&quot; & string1 & &quot;<li>&quot; & string2

Hope this helps a bit also take into account it was written on the fly


 
Thank you very much, I don't have time to try it right now, but it's a very good start.
 
Had a look at this and this works I think how you want it.
<%
strText = &quot;MEL, NICK, MICK, MARK&quot;
arrWords = Split(strText, &quot;,&quot;)


Dim iLen
iLen = UBound(arrWords)


Const iTwo = 2
iLen = iLen / iTwo

For iCounter = 0 to UBound(arrWords)
Dim string1, string2
if( iCounter < iLen ) then
string1 = string1 + arrWords(iCounter) & &quot;, &quot;
else
string2 = string2 + arrWords(iCounter) & &quot;, &quot;
end if
Next
i = Len(string1) - 2
j = Len(string2) -2
string1 = Left(string1, i )
string2 = Left(string2, j )

Response.write &quot;<li>&quot; & string1 & &quot;<li>&quot; & string2



%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top