Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead tokens from a delimited string
Keywordstoken, delimited string, string
CategoriesStrings
 
The GetToken function returns a token from a string. If its parameter is blank, it returns the next token from the previous string. If the parameter is not blank, GetToken returns the new string's first token.

GetToken uses InStr to find the first occurrance of the delimiter in the string. It returns the text to the left and removes that token from the Static string txt.

 
' Return a token. If new_txt is blank, return the
' next token from the previous string. If new_txt
' is not blank, return its first token.
Public Function GetToken(new_txt As String, delimiter As _
    String) As String
Static txt As String
Dim pos As Integer

    ' Save new text.
    If new_txt <> "" Then txt = new_txt

    pos = InStr(txt, delimiter)
    If pos < 1 Then pos = Len(txt) + 1
    GetToken = Left$(txt, pos - 1)
    pos = Len(txt) - pos + 1 - Len(delimiter)
    If pos < 1 Then
        txt = ""
    Else
        txt = Right$(txt, pos)
    End If
End Function
 
The following code demonstrates the GetToken function.
 
Private Sub Command1_Click()
Dim txt As String
Dim token As String

    txt = GetToken(Text1.Text, ";") & vbCrLf
    Do
        token = GetToken("", ";")
        If token = "" Then Exit Do
        txt = txt & token & vbCrLf
    Loop
    MsgBox txt
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated