Access/VBA Library


Split a string into an array of tokens.


Tokenizer( TokenString, TokenSeparators-1 [, TokenSeparators-2 [,...[, TokenSeparators-N]]] )
Name Type Description
TokenString Variant Required. A String expression containing the text to be parsed.
TokenSeparators Variant Required. One or more String expressions containing the separators.
Return value

Variant : Array of String . Each element contains a token.

Function Tokenizer(ByVal TokenString, ParamArray TokenSeparators())
    Dim lb As Long
    Dim ub As Long
    Dim i As Long
    ' Get boundaries of the separators array
    lb = LBound(TokenSeparators)
    ub = UBound(TokenSeparators)
    ' Replace every separator by a NULL-character
    For i = lb To ub
        TokenString = Replace(TokenString, TokenSeparators(i), vbNullChar)
    Next i
    ' Prevent empty tokens by multiple NULL-characters
    While InStr(TokenString, vbNullChar & vbNullChar) > 0
        TokenString = Replace(TokenString, vbNullChar & vbNullChar, vbNullChar)
    ' Prevent from first empty token
    If Left(TokenString, 1) = vbNullChar Then TokenString = Mid(TokenString, 2)
    ' Prevent from last empty token
    If Right(TokenString, 1) = vbNullChar Then TokenString = Left(TokenString, Len(TokenString) - 1)
    ' Split string and return value
    Tokenizer = Split(TokenString, vbNullChar)
End Function