TLsplit Function

This function was designed by my Co-worker Tim Lasek after I asked him to make it because
of things that I didn't like about the built in vbscript function called
"split". I primarily use it to take arrays passed from the HTTP HEADER and put
them back together, but Tim decided to enhance it by letting you specify the delimiter and
by having the array dynamically declare itself so that it is always the exact size it
needs to be. This makes this function very versatile. This also handle null values better
than split does and overall I like it better..
Function TLsplit(StrToSplit, Delim)
Usage: TestArray = TLsplit("This,is,a,dynamic,split,function",
",")
Result:
TestArray(0) = 6 TestArray(1) = This TestArray(2) = is TestArray(3) = a TestArray(4) = dynamic TestArray(5) = split TestArray(6) = function
No need to Dim() the array before calling the function.
Array(0) = Number of elements in the array.
If StrToSplit = NULL Then Array(0) = 0.
Checks for Delim not found.
Don't use " for Delim.
Below is the Function Source Code and Example Usage Code
<%
Function TLsplit(StrToSplit,Delim)
Dim StrArray()
If StrToSplit = "" Then
ReDim StrArray(1)
StrArray(0) = 0
StrArray(1) = ""
TLsplit = StrArray
Else
dcount = 0
For TLsI = 1 To Len(StrToSplit)
If Mid(StrToSplit,TLsI,1) = Delim Then dCount = dCount + 1
Next
If dCount = 0 Then
ReDim StrArray(1)
StrArray(0) = 1
StrArray(1) = StrToSplit
TLsplit = StrArray
Else
ReDim StrArray(dCount + 1)
StrArray(0) = dCount + 1
TempArray = Split(StrToSplit,Delim)
For TLsI = 1 To dCount + 1
StrArray(TLsI) = Trim(TempArray(TLsI - 1))
TLsplit = StrArray
Next
End If
End If
End Function
%>
<% RandomVariable =
"This,is,a,dynamic,split,function" %>
<%
TestArray = TLsplit(RandomVariable, ",")
For I = 1 To TestArray(0)
Response.Write("TestArray(" & I & ") = " & TestArray(I)
& "<br>")
Next
%>
|