vb小问题,不吝赐教

来源:百度知道 编辑:UC知道 时间:2024/06/01 06:39:49
我想从 "abcd abcd abcd abcd" 这个字符串中取得第二个空格的位置 ,可是instr只能取得第一个空格的位置,第二个空格怎么取得?大侠指点一下

Private Sub Command1_Click()
Print fun("abcd abcd abcd abcd", " ")
End Sub

Private Function fun(s1 As String, s2 As String) As Integer
Dim i As Integer
Dim n As Integer
i = 1
n = InStr(s1, s2)
Do Until i = 2
If n = 0 Then Exit Do
i = i + 1
n = InStr(n + 1, s1, s2)
Loop
If n <> 0 Then
fun = n
Else
MsgBox "the second one can not be found"
End If
End Function

'QQ:659327515

dim a() as string
a=Split("abcd abcd abcd abcd"," ")

这样一来结果是a(0)=abcd a(1)=abcd a(2)=abcd,反正就是以空格分开

稍麻烦点,你可以试试如下代码:
Dim r As String, i As Integer, t As String
r = "abcd abcd abcd abcd"
i = InStr(r, " ")
t = Right(r, Len(r) - i)
i = i + InStr(t, " ")

Dim r As String, i As Integer
r = "abcd abcd abcd abcd"
i = InStrRev(r, " abcd abcd")