新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Dim strAll As String
Dim strArr() As String
Dim i As Integer
strAll = "a b c"
strArr = Split(strAll)
For i = 0 To UBound(strArr)
Debug.Print strArr(i)
Next
strAll = "ABC"
strArr = Split(strAll)
For i = 0 To UBound(strArr)
Debug.Print strArr(i)
Next
strAll = "1-2-3"
strArr = Split(strAll, "-")
For i = 0 To UBound(strArr)
Debug.Print strArr(i)
Next
Debug.Print "over"
End Sub
运行结果:
a
b
c
ABC
1
2
3
over
讨论一下最后有时会多一个空格的问题:
Private Sub Command1_Click()
Dim strAll As String
Dim strArr() As String
Dim i As Integer
strAll = "t=="
strArr = Split(strAll, "=")
Debug.Print "UBound=" & UBound(strArr)
For i = 0 To UBound(strArr)
Debug.Print strArr(i)
Next
Debug.Print "over"
End Sub
运行结果:
UBound=2
t
over
可以看到,一共是0,1,2三行,为什么?
因为VB6会在字符串尾部多加一个分割符,类似:
strArr = Split(strAll & "=", "=")
用纯VB6代码完成函数Split的功能,代码如下:
Private Sub Command1_Click()
Dim strDelimiter$
strDelimiter = "="
s_SpPrint "t", strDelimiter
s_SpPrint "t=2", strDelimiter
s_SpPrint "t==3X=", strDelimiter
s_SpPrint "t===4", strDelimiter
strDelimiter = "=a"
s_SpPrint "t", strDelimiter
s_SpPrint "t=a2", strDelimiter
s_SpPrint "t=a=a3X=a", strDelimiter
s_SpPrint "t=a=a=a4", strDelimiter
Debug.Print "over"
End Sub
Private Sub s_SpPrint(strAll As String, strDelimiter As String)
Dim i&
Dim strSP() As String
Debug.Print "分隔符:" & strDelimiter & " 原始字符串:" & strAll
strSP = f_Split(strAll, strDelimiter)
Debug.Print "LBound=" & LBound(strSP) & " UBound=" & UBound(strSP)
For i = LBound(strSP) To UBound(strSP)
Debug.Print "strSP(" & i&; ")=" & strSP(i)
Next
Debug.Print "----------------"
End Sub
Function f_Split(strAll As String, strDelimiter As String) As String()
Dim lngStartPos&, lngFindPos&, lngNext&, lenDelimiter&, lngCount&
Dim strAllTemp$
Dim strResult() As String
ReDim strResult(0)
strAllTemp = strAll & strDelimiter '添加一个分隔符在最后
lenDelimiter = Len(strDelimiter)
lngNext = 1
lngCount = 0
lngFindPos = InStr(strAllTemp, strDelimiter)
If lngFindPos = 0 Then
'没找到
strResult(lngCount) = strAll
f_Split = strResult
Exit Function
End If
strResult(lngCount) = Mid(strAllTemp, lngNext, lngFindPos - 1)
lngNext = lngFindPos + lenDelimiter
lngFindPos = InStr(lngFindPos + lenDelimiter, strAllTemp, strDelimiter)
Do While lngFindPos > 0
lngCount = lngCount + 1
ReDim Preserve strResult(lngCount)
strResult(lngCount) = Mid(strAllTemp, lngNext, lngFindPos - lngNext)
lngNext = lngFindPos + lenDelimiter
lngFindPos = InStr(lngNext, strAllTemp, strDelimiter)
Loop
f_Split = strResult
End Function
运行结果:
分隔符:= 原始字符串:t
LBound=0 UBound=0
strSP(0)=t
----------------
分隔符:= 原始字符串:t=2
LBound=0 UBound=1
strSP(0)=t
strSP(1)=2
----------------
分隔符:= 原始字符串:t==3X=
LBound=0 UBound=3
strSP(0)=t
strSP(1)=
strSP(2)=3X
strSP(3)=
----------------
分隔符:= 原始字符串:t===4
LBound=0 UBound=3
strSP(0)=t
strSP(1)=
strSP(2)=
strSP(3)=4
----------------
分隔符:=a 原始字符串:t
LBound=0 UBound=0
strSP(0)=t
----------------
分隔符:=a 原始字符串:t=a2
LBound=0 UBound=1
strSP(0)=t
strSP(1)=2
----------------
分隔符:=a 原始字符串:t=a=a3X=a
LBound=0 UBound=3
strSP(0)=t
strSP(1)=
strSP(2)=3X
strSP(3)=
----------------
分隔符:=a 原始字符串:t=a=a=a4
LBound=0 UBound=3
strSP(0)=t
strSP(1)=
strSP(2)=
strSP(3)=4
----------------
over