Public Function readlin(ByVal nl As String, ByRef data() As String) As String
Dim i, j As Integer
Dim d(20), s As String
s = " "
i = InStr(1, nl, s)
d(0) = 1
j = 1
While i 0
'd()中记录的","出现的位置
d(j) = i
i = InStr(i + 1, nl, s)
j = j + 1
Wend
d(j) = Len(nl) + 1
nn = 0
For i = 1 To j
If d(i) - d(i - 1) > 1 Then
data(nn) = Trim(Mid(nl, d(i - 1), d(i) - d(i - 1)))
nn = nn + 1
End If
Next i
End Function
以上程序如下:
Public Function SplitStr(MainStr As String, Pos As Integer) As String
Dim l, i, j, n, k
Dim StrVal(0 To 100) As Variant
Dim MyStr As String
n = 1: j = 0: i = 0
MyStr = " "
l = Len(MainStr)
While i < l
k = InStr(n, MainStr, " ")
If k = 0 Then k = l + 1
MyStr = Mid(MainStr, n, k - n)
n = InStr(n, MainStr, " ")
If MyStr = "" Then
n = n + 1
i = i + 1
Else
j = j + 1
i = i + Len(MyStr)
StrVal(j) = MyStr
End If
Wend
SplitStr = StrVal(Pos)
End Function
Sub test()
SplitStr("1 2 3", 1)
End Sub