InStr 函數 返回某字符串在另一字符串中第一次出現的位置。 start 可選項。數值表達式,用于設置每次搜索的開始位置。如果省略,將從第一個字符的位置開始搜索。如果 start 包含 Null,則會出現錯誤。如果已指定 compare,則必須要有 start 參數。 string1 必選項。接受搜索的字符串表達式。 string2 必選項。要搜索的字符串表達式。 compare 可選項。指示在計算子字符串時使用的比較類型的數值。有關數值,請參閱“設置”部分。如果省略,將執行二進制比較。 compare 參數可以有以下值: InStr 函數返回以下值: 下面的示例利用 InStr 搜索字符串: 注意 InStrB 函數使用包含在字符串中的字節數據,所以 InStrB 返回的不是一個InStr([start, ]string1, string2[, compare])
參數
設置
常數
值
描述
vbBinaryCompare
0
執行二進制比較。
vbTextCompare
1
執行文本比較。
返回值
如果
InStr 返回
string1 為零長度
0
string1 為 Null
Null
string2 為零長度
start
string2 為 Null
Null
string2 沒有找到
0
在 string1 中找到 string2
找到匹配字符串的位置
start > Len(string2)
0
說明
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
MyPos = Instr(4, SearchString, SearchChar, 1)
' A textual comparison starting
at position 4. Returns 6.MyPos = Instr(1, SearchString, SearchChar, 0)
' A binary comparison starting
at position 1. Returns 9. MyPos = Instr(SearchString, SearchChar)
' Comparison is binary by default
(last argument is omitted). Returns 9.MyPos = Instr(1, SearchString, "W")
' A binary comparison starting
at position 1. Returns 0 ("W" is not found).
字符串在另一個字符串中第一次出現的字符位置,而是字節位置。