I wrote this. Yes, I know it’s VB6. Yes, it is production code, and, yeah, I know it uses gotos. I am a lazy, evil beast …
So show me (and the rest of us) how it should be written
Public Function SplitString(ByVal sText As Variant) As Variant
Dim nHere As Long
Dim cHere As String * 1
Dim aRes As Variant
Dim nRes As Long
Dim bInquote As Boolean
Dim sString As String
ReDim aRes(0)
nHere = 1
nRes = 0
Do
If nHere > Len(sText) Then Exit Do
cHere = Mid$(sText, nHere, 1)
If cHere = Chr$(32) Then
If bInquote Then
sString = sString & cHere
GoTo nextChar
End If
If sString <> vbNullString Then
aRes(nRes) = sString
sString = vbNullString
nRes = nRes + 1
ReDim Preserve aRes(nRes)
End If
GoTo nextChar
ElseIf cHere = Chr$(34) Then
bInquote = Not bInquote
GoTo nextChar
Else
sString = sString & cHere
End If
nextChar:
nHere = nHere + 1
Loop
If sString <> vbNullString Then
aRes(nRes) = sString
End If
SplitString = aRes
End Function
By the way, it splits a string into an array. The elements in the string may be quoted.
I agree that this particular bit of logic should be clearly and easily implemented using Split() and Join() operations. While one can always write a long run of inline code that improves on them in speed there are two reasons not to:
Example:
BTW: Kudos to anyone who can fix the perverse code-quoting markup this site uses in my example above.
Edit: Nevermind, I winkled it out. The bulletted list gave the parser a spasm.