InStr()
函式返回一個字串第一次出現在一個字串,從左到右搜尋。返回搜尋到的字元索引位置。
InStr([start,]string1,string2[,compare])
參考以下程式碼 -
Private Sub Constant_demo_Click()
Dim Var As Variant
Var = "Microsoft VBScript"
MsgBox ("Line 1 : " & InStr(1, Var, "s"))
MsgBox ("Line 2 : " & InStr(7, Var, "s"))
MsgBox ("Line 3 : " & InStr(1, Var, "f", 1))
MsgBox ("Line 4 : " & InStr(1, Var, "t", 0))
MsgBox ("Line 5 : " & InStr(1, Var, "i"))
MsgBox ("Line 6 : " & InStr(7, Var, "i"))
MsgBox ("Line 7 : " & InStr(Var, "VB"))
End Sub
當你執行上面的函式時,它會產生下面的輸出。
Line 1 : 6
Line 2 : 0
Line 3 : 8
Line 4 : 9
Line 5 : 2
Line 6 : 16
Line 7 : 11