SplitVB6




Public Function Split(Expression As String, _
Optional Delimiter As String, _
Optional Limit As Long = -1, _
Optional Compare As VbCompareMethod) _
As Variant
Dim tmpSplit() As String
Dim i As Integer
Dim tmpToken As String
Dim tmpExp As String
tmpExp = Expression
If IsEmpty(Delimiter) Then
ReDim tmpSplit(0)
tmpSplit(0) = tmpExp
Goto EXIT_ROUTINE
End If
If GetToken(tmpExp, Delimiter, tmpToken, Limit, Compare) Then
ReDim tmpSplit(i)
tmpSplit(i) = tmpToken
i = i + 1
End If
Do While GetToken("", Delimiter, tmpToken, Limit, Compare)
ReDim Preserve tmpSplit(i)
tmpSplit(i) = tmpToken
i = i + 1
If i = Limit Then
End If
Loop
EXIT_ROUTINE:
'This is to remove the blank array members at the end

'as VB6 Split function.

For i = UBound(tmpSplit) To LBound(tmpSplit) Step -1
If tmpSplit(i) = "" Then
ReDim Preserve tmpSplit(i - 1)
Else
Exit For
End If
Next i
Split = tmpSplit
End Function

'Return the next token through the tokenparameter.

'If new_txt is not blank, return the first token

'from it. If new_txt is blank, return the next

'token from the original value of new_txt.

'Return False if there was no token leftto process.

Public Function GetToken(ByVal new_txt As String, _
ByVal Delimiter As String, token As String, _
new_limit As Long, _
Optional Compare As VbCompareMethod) As Boolean
Static txt As String
Static Limit As Long
Dim pos As Integer
'Limit is reached pass rest of string.

Limit = Limit + 1
If Limit = new_limit Then
token = txt
txt = ""
Limit = 0
GetToken = True
Exit Function
ElseIf new_limit = -1 Then
Limit = 0
End If
'Save new text for next time.

If Len(new_txt) > 0 Then txt = new_txt
'If txt is empty, there are no more tokens.

If Len(txt) = 0 Then
GetToken = False
Limit = 0
Else
GetToken = True
'Find the next delimiter.

pos = InStr(1, txt, Delimiter, Compare)
If pos < 1 Then
'The delimiter was not found. Return

'the rest of the string as the token

token = txt
txt = ""
Else
'The delimiter was found. Return

'the next token.

token = Left$(txt, pos - 1)
txt = Mid$(txt, pos + Len(Delimiter))
End If
End If
End Function

Inputs:Expression As String

Optional Delimiter As String
Optional Limit As Long = -1
Optional Compare As VbCompareMethod

Returns:Base 0 Variant Array with 'Limit' members













( splitvb6.html )- by Paolo Puglisi - Modifica del 17/12/2023