VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Token Routine - sGetToken

by William M. Rawls (3 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 31st May 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Token Routine - sGetToken

API Declarations


' This is the first template that has been
' submitted from inside Slice and Dice
' I can now import a vbcode.com code snippet
' as a Slice and Dice Template
' And submite a Slice and Dice Template
' as a vbcode.com code snippet.
'
' Cool, no ?


Rate Token Routine - sGetToken



' Synopsis          Returns the Nth Token from sAllTokens delimited by sDelim
'
' Parameters
'
'   sAllTokens                 (I) Required. The string containing all the tokens
'   iToken                      (I) Optional. The index of the token to return
'                                   DEFAULT = 1
'   siDelim                     (I) Optional. The delimiter string that separates
'                                   the tokens. DEFAULT = " "
' Description
'  For the following:
'    sAllTokens         iToken   sDelim  Returns       Notes
'   "William M Rawls"    1       " "     "William"      First word
'   "William M Rawls"    2       " "     "M"            Second word
'   "William M Rawls"    3       " "     "Rawls"        Third word
'   "William M Rawls"    4       " "     ""             No forth word
'   "William M Rawls"    0       " "     ""             Zeroth token is always empty
'   "William M Rawls"   -1       " "     ""             Negative tokesn always empty
'   "William M Rawls"    1       ""      ""             No delimiter ? Token empty

Public Function sGetToken(ByVal sAllTokens As String, Optional ByVal iToken As Integer = 1, Optional ByVal sDelim As String = " ") As String
    Static iCurTokenLocation As Long ' Character position of the first delimiter string
    Static nDelim As Integer            ' Length of the delimiter string
    nDelim = Len(sDelim)

    If iToken < 1 Or nDelim < 1 Then
     ' Negative or zeroth token or empty delimiter strings mean an empty token
       Exit Function
    ElseIf iToken = 1 Then
     ' Quickly extract the first token
       iCurTokenLocation = InStr(sAllTokens, sDelim)
       If iCurTokenLocation > 1 Then
          sGetToken = Left(sAllTokens, iCurTokenLocation - 1)
       ElseIf iCurTokenLocation = 1 Then
          sGetToken = ""
       Else
          sGetToken = sAllTokens
       End If
       Exit Function
    Else
     ' Find the Nth token
       Do
          iCurTokenLocation = InStr(sAllTokens, sDelim)
          If iCurTokenLocation = 0 Then
             Exit Function
          Else
             sAllTokens = Mid(sAllTokens, iCurTokenLocation + nDelim)
          End If
          iToken = iToken - 1
       Loop Until iToken = 1

     ' Extract the Nth token (Which is the next token at this point)
       iCurTokenLocation = InStr(sAllTokens, sDelim)
       If iCurTokenLocation > 0 Then
          sGetToken = Left(sAllTokens, iCurTokenLocation - 1)
          Exit Function
       Else
          sGetToken = sAllTokens
          Exit Function
       End If
    End If
End Function


Download this snippet    Add to My Saved Code

Token Routine - sGetToken Comments

No comments have been posted about Token Routine - sGetToken. Why not be the first to post a comment about Token Routine - sGetToken.

Post your comment

Subject:
Message:
0/1000 characters