我写了这个。是的,我知道它是 VB6。是的,它是生产代码,而且,是的,我知道它使用 goto。我是一只懒惰、邪恶的野兽……
所以告诉我(和我们其他人)应该如何写
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
顺便说一句,它将一个字符串拆分为一个数组。字符串中的元素可以被引用。