by William M. Rawls (3 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 31st May 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Token Routine - sBefore
API Declarations
~~GotoModule modGeneral
'
' Parameters
'
' sAllTokens (I) Required. The string containing all the tokens
' iToken (I) Optional. The index of the token to use as a "before" ref
' DEFAULT = 2
' siDelim (I) Optional. The delimiter string that separates
' the tokens. DEFAULT = " " (Space)
' Description
' For the following:
' sAllTokens iToken sDelim Returns Notes
' "William M Rawls" 1 " " "" Before the first word (nothing)
' "William M Rawls" 2 " " "William" Before the second word
' "William M Rawls" 3 " " "William M" Before the third word
' "William M Rawls" 0 " " "" Before zeroth token (nothing)
' "William M Rawls" -1 " " "" Negative tokens act same as zero
' "William M Rawls" 1 "" "" Same as one
' ********************************************************************************Public Function sBefore(ByVal sAllTokens As String, Optional ByVal iToken As Integer = 2, 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
Static sReturned As String
nDelim = Len(sDelim)
If iToken < 2 Or nDelim < 1 Then
' First, Zeroth, or Negative tokens or empty delimiter strings mean an empty string returned
sBefore = ""
Exit Function
ElseIf iToken = 2 Then
' Quickly extract the first token
sBefore = sGetToken(sAllTokens, 1, sDelim)
Exit Function
Else
' Find the Nth token
Do
iCurTokenLocation = InStr(sAllTokens, sDelim)
If iCurTokenLocation = 0 Or iToken = 1 Then
sBefore = sReturned
sReturned = ""
Exit Function
ElseIf Len(sReturned) = 0 Then
sReturned = Left(sAllTokens, iCurTokenLocation - 1)
Else
sReturned = sReturned & sDelim & Left(sAllTokens, iCurTokenLocation - 1)
End If
sAllTokens = Mid(sAllTokens, iCurTokenLocation + nDelim)
iToken = iToken - 1
Loop
End If
End Function