Not a Member Yet,
Click here to Register

ID: 449
Viewed: 3400
Added: Aug 19, 2002
Version:
Snippet uploaded by: snippet
Written By: Clint LaFever
Demo: Sorry, no demo



User Rated at: 0 Stars
Rate This:

Thank you for your vote. Please wait...

It appears you already voted for this snippet

It appears your vote value was empty

Code to be used with delimited strings. Functions to count the number of substrings [tokens] and to retrieve the tokens from the delimited string are provided.

Highlight all by clicking in box
<!---Declaration--->
'------------------------------------------------------------ 
' Author: Clint M. LaFever [clint.m.lafever@cpmx.saic.com]
' Purpose: Gets the first token off a delimited string.
' Returns the token and changes the passed string
' with the first token removed.
' Parameters: delimited string and delimiter
' Returns: First Token off delimited string
'------------------------------------------------------------

Highlight All
<!---Code--->

Function GetToken(sSource As String, ByVal sDelim As String) As String
Dim iDelimPos As Integer
On Error GoTo ErrorGetToken
'------------------------------------------------------------
' Find the first delimiter
'------------------------------------------------------------
iDelimPos = InStr(1, sSource, sDelim)
'------------------------------------------------------------
' If no delimiter was found, return the existing
' string and set the source to an empty string.
'------------------------------------------------------------
If (iDelimPos = 0) Then
GetToken = Trim$(sSource)
sSource = ""
'------------------------------------------------------------
' Otherwise, return everything to the left of the
' delimiter and return the source string with it
' removed.
'------------------------------------------------------------
Else
GetToken = Trim$(Left$(sSource, iDelimPos - 1))
sSource = Mid$(sSource, iDelimPos + 1)
End If
Exit Function
ErrorGetToken:
MsgBox Err & ":Error in GetToken() Function. Error Message:" & Error(Err), 48, "Warning"
Exit Function
End Function
'------------------------------------------------------------
' Author: Clint M. LaFever [clint.m.lafever@cpmx.saic.com]
' Purpose: Counts the number of tokens that are in a delimited ' string. ' Parameters: delimited string and delimiter
' Returns: Number of tokens.
'------------------------------------------------------------
Function CountTokens(ByVal sSource As String, ByVal sDelim As String) As Integer
Dim iDelimPos As Integer
Dim iCount As Integer
On Error GoTo ErrorCountTokens
'------------------------------------------------------------
' Number of tokens = 0 if the source string is ' empty
'------------------------------------------------------------
If sSource = "" Then CountTokens = 0
'------------------------------------------------------------
' Otherwise number of tokens = number of delimiters
' 1
'------------------------------------------------------------
Else
iDelimPos = InStr(1, sSource, sDelim)
Do Until iDelimPos = 0
iCount = iCount + 1
iDelimPos = InStr(iDelimPos + 1, sSource, sDelim)
Loop
CountTokens = iCount + 1
End If
Exit Function
ErrorCountTokens:
MsgBox Err & ":Error in CountTokens() Function. Error Message:" & Error(Err), 48, "Warning"
Exit Function
End Function
;


No Comments to show

Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Replying to a Comment...


Adding your comment. Please wait...

Thanks for adding your comment!. After further review it will be added.

There was a problem adding your comment. Please try again.

Please complete all the fields in the form before sending.

© 2002 - 2024 snippetlibrary.com All Rights Reserved. Conditions
Do NOT follow this link or you will be banned from the site!