Search Tools Links Login

Easy Tokenizer

Break up variables in a string, whatever separator is used.

Original Author: Paul Crowdy


String to be Tokenized, Separator between variables


List of individual variables

API Declarations

Public Type TokenList
Tokens() As String
TokenCount As Integer
End Type
Public Function Tokenize(strString As String, strSeparator As String) As TokenList

Dim iCount As Integer
Dim iStart As Integer
Dim iTokens As Integer
ReDim Tokenize.Tokens(0)
iTokens = 0
iCount = 1
iStart = 1

Do Until iCount = Len(strString)

If Mid$(strString, iCount, Len(strSeparator)) = strSeparator Then
ReDim Preserve Tokenize.Tokens(iTokens + 1)
Tokenize.Tokens(iTokens) = Mid$(strString, iStart, iCount - iStart)
iStart = iCount + Len(strSeparator)
iTokens = iTokens + 1
End If

iCount = iCount + 1


Tokenize.Tokens(iTokens) = Mid$(strString, iStart)
Tokenize.TokenCount = iTokens + 1

End Function


Private Sub Form_Load()
  Dim tk As TokenList
  Dim strTest As String
  Dim strSeparator As String
  strTest = "String, Tokenization, By, Paul, Crowdy,"
  strSeparator = ", "
  tk = Tokenize(strTest, strSeparator)
  For i = 0 To tk.TokenCount - 1
    MsgBox "Token " & i + 1 & " = " & tk.Tokens(i), vbInformation, strTest
  Next i
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 92 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.