Split String

The sub Click is the example call, tokStrInArray and strTok$ are the
Sub Click(Source As Button)
Dim ArrayList$(), i
tokStrInArray ArrayList$(), "One;Two;Three", ";"
For i = Lbound(ArrayList$) To Ubound(ArrayList$)
Msgbox ArrayList$(i)
Next i
End Sub

Sub tokStrInArray (Array$(), st$, Delimiters$)

Dim i As Integer, j As Integer, Token$
ReDim tmp$(640) ' temporary array

' Invoke StrTok$ with the string to tokenize.
Token$ = strTok$(st$, Delimiters$)
i = 0
Do While Token$ <> ""
' Transfer to temporary array
i = i + 1
tmp$(i) = Token$
' Call StrTok$ with a null string so it knows this
' isn't the first call.
Token$ = strTok$("", Delimiters$)
If i >= 640 Then Exit Do

' Redimension CLArgs$() and transfer values from tmp$()
ReDim Array$(1 To i)
For j = 1 To i
Array$(j) = tmp$(j)
Next j

End Sub

Function strTok$ (Srce$, Delim$)

Dim BegPos%, EndPos%, Ln%
Static start%, SaveStr$

' If first call, make a copy of the string.
If Srce$ <> "" Then
start% = 1: SaveStr$ = Srce$
End If

BegPos% = start%: Ln% = Len(SaveStr$)
' Look for start of a token (character that isn't delimiter).
While BegPos% <= Ln% And InStr(Delim$, Mid$(SaveStr$, BegPos%, 1)) <> 0
BegPos% = BegPos% + 1
' Test for token start found.
If BegPos% > Ln% Then
strTok$ = "": Exit Function
End If
' Find the end of the token.
EndPos% = BegPos%
While EndPos% <= Ln% And InStr(Delim$, Mid$(SaveStr$, EndPos%, 1)) = 0
EndPos% = EndPos% + 1
strTok$ = Mid$(SaveStr$, BegPos%, EndPos% - BegPos%)
' Set starting point for search for next token.
start% = EndPos%

End Function

This was first published in November 2000

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.