返回某字符串在另一字符串中第一次出現(xiàn)的位置。
InStr([start, ]string1, string2[, compare])
compare 參數(shù)可以有以下值:
常數(shù) | 值 | 描述 |
---|---|---|
vbBinaryCompare | 0 | 執(zhí)行二進(jìn)制比較。 |
vbTextCompare | 1 | 執(zhí)行文本比較。 |
InStr 函數(shù)返回以下值:
如果 | InStr 返回 |
---|---|
string1 為零長(zhǎng)度 | 0 |
string1 為 Null | Null |
string2 為零長(zhǎng)度 | start |
string2 為 Null | Null |
string2 沒有找到 | 0 |
在 string1 中找到 string2 | 找到匹配字符串的位置 |
start > Len(string2) | 0 |
下面的示例利用 InStr 搜索字符串:
Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' 要搜索的字符串。 SearchChar = "P" ' Search for "P". MyPos =Instr(
4,
SearchString,
SearchChar,
1)
' 在位置 4 進(jìn)行的文本比較。返回 6。 MyPos =Instr(
1,
SearchString,
SearchChar,
0)
' 在位置 1 進(jìn)行的二進(jìn)制比較。返回 9。 MyPos =Instr(
SearchString,
SearchChar)
' 默認(rèn)情況下,進(jìn)行的是二進(jìn)制比較(省略了最后的參數(shù))。返回 9。 MyPos =Instr(
1,
SearchString,
"W")
' 在位置 1 進(jìn)行的二進(jìn)制比較。返回 0(找不到 "W")。
注意 InStrB 函數(shù)使用包含在字符串中的字節(jié)數(shù)據(jù),所以 InStrB 返回的不是一個(gè)字符串在另一個(gè)字符串中第一次出現(xiàn)的字符位置,而是字節(jié)位置。