VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



GetToken

by Troy DeMonbreun (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

The following code is a Visual Basic function that returns a specific "token" (section/substring of data) from a delimited string list. The function accepts the index of the desired token and also the delimiter as specified by the programmer.

Inputs
Requires : [string] delimited data, [integer] index of desired section, [string] delimiter (1 or more chars)
Assumes
Examples: GetToken("[email protected]", 2, "@") returns "hotmail.com" GetToken("first,second,third", 2, ",") returns "second" GetToken("111, 222, 333", 3, ", ") returns "333" GetToken("line1" + vbCrLf + "line2" + vbCrLf + "line3", 2, vbCrLf) returns "line2"
Code Returns
Returns : [string] "Token" (section of data) from a list of delimited string data
API Declarations

Rate GetToken

Function GetToken(ByVal strVal As String, intIndex As Integer, _
strDelimiter As String) As String
'------------------------------------------------------------------------
' Author  : Troy DeMonbreun ([email protected])
'
' Returns : [string] "Token" (section of data) from a list of
'      delimited string data
'
' Requires : [string] delimited data,
'      [integer] index of desired section,
'      [string] delimiter (1 or more chars)
'
' Examples : GetToken("[email protected]", 2, "@") returns "hotmail.com"
'      GetToken("123-45-6789", 2, "-") returns "45"
'      GetToken("first,middle,last", 3, ",") returns "last"
'
' Revised : 12/22/1998
'------------------------------------------------------------------------
Dim strSubString() As String
Dim intIndex2 As Integer
Dim i As Integer
Dim intDelimitLen As Integer
intIndex2 = 1
i = 0
intDelimitLen = Len(strDelimiter)
Do While intIndex2 > 0
  
ReDim Preserve strSubString(i + 1)
    
intIndex2 = InStr(1, strVal, strDelimiter)
  
If intIndex2 > 0 Then
strSubString(i) = Mid(strVal, 1, (intIndex2 - 1))
strVal = Mid(strVal, (intIndex2 + intDelimitLen), Len(strVal))
Else
strSubString(i) = strVal
End If
    
i = i + 1
    
Loop
If intIndex > (i + 1) Or intIndex < 1 Then
GetToken = ""
Else
GetToken = strSubString(intIndex - 1)
End If
End Function

Download this snippet    Add to My Saved Code

GetToken Comments

No comments have been posted about GetToken. Why not be the first to post a comment about GetToken.

Post your comment

Subject:
Message:
0/1000 characters