by Agent153 (2 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating:
(3 Votes)
This code duplicates the functionality of VB6's split function.
Inputs
TheString$: The string to be parsed
Optional Delim$: The dilimeter to parse TheString$ by
Optional Limit: The maximum number of elements to return.
Assumes
If Delim is ommited or is not found, a single element array is returned.
To have Split return all of the substring, Limit should be set to -1.
Code Returns
Variant containing an array
Side Effects
If your program is migrated to VB6, this code will need to be removed, as it shares the same functionality (and name) of a VB6 intrisync function.
Function Split(TheString As String, Optional Delim As String, Optional Limit As Long = -1) As Variant
'Duplicates the functionality of the vb6 counterpart.
'Unfortunately, I was unable to include the vbcompare part of the vb6 funtionality.
'Just use Option Campare at the beggining of this module.
Dim dynArray() As Variant
If Len(Delim) > 0 Then
Dim ArrCt%
Dim CurPos%
Dim LenAssigned%
Dim CurStrLen%
ArrCt% = 0
CurPos% = 1
LenAssigned% = 1
CurStrLen% = Len(TheString$)
Do
ReDim Preserve dynArray(0 To ArrCt%)
CurStrLen% = (InStr(CurPos%, TheString$, Delim$) - CurPos%)
If CurStrLen% < 0 Then
dynArray(ArrCt%) = Right$(TheString$, (Len(TheString$) - (LenAssigned% - 1)))
Exit Do
Else
dynArray(ArrCt%) = Mid$(TheString$, CurPos%, CurStrLen%)
End If
LenAssigned% = LenAssigned% + (Len(dynArray(ArrCt%)) + Len(Delim$))
CurPos% = LenAssigned%
ArrCt% = ArrCt% + 1
If Not Limit = -1 Then
If ArrCt = Limit Then Exit Do
End If
Loop
Split = dynArray
Else
'duplicate the functionality more acuratley
ReDim dynArray(0 To 0)
dynArray(0) = TheString
Split = dynArray
End If
End Function