注意函数 FindStrN
Option Explicit
Private Sub Command1_Click()
Dim s As String
s = "abs01b902h9dso0h2e70de210j0q"
Dim x As Integer
'第1个"0"的位置
x = FindStrN(s, "0", 1)
Print x
'第2个"0"的位置
x = FindStrN(s, "0", 2)
Print x
'第3个"0"的位置
x = FindStrN(s, "0", 3)
Print x
'第4个"0"的位置
x = FindStrN(s, "0", 4)
Print x
'第5个"0"的位置
x = FindStrN(s, "0", 5)
Print x
'第6个"0"的位置
x = FindStrN(s, "0", 6)
Print x
'第7个"0"的位置,不存在,返回-1
x = FindStrN(s, "0", 7)
Print x
End Sub
'查找 src 中第n个c出现的位置
'如果没找对,返回-1
Function FindStrN(ByVal src As String, _
ByVal c As String, _
ByVal n As Integer) As Integer
Dim i As Integer, m As Integer
i = InStr(1, src, c)
m = 0
Do While i > 0
m = m + 1
If m = n Then Exit Do
i = InStr(i + 1, src, c)
Loop
If i > 0 And m = n Then
FindStrN = i
Else
FindStrN = -1
End If
End Function