VBScript InStr 函數


VBScript 參考手冊 完整的 VBScript 參考手冊

InStr 函數返回一個字串在另一個字串中首次出現的位置。

InStr 函數返回下麵的值:

  • 如果 string1 為 "" - InStr 返回 0
  • 如果 string1 為 Null - InStr 返回 Null
  • 如果 string2 為 "" - InStr 返回 start
  • 如果 string2 為 Null - InStr 返回 Null
  • 如果 string2 沒有找到 - InStr 返回 0
  • 如果在 string1 中找到 string2 - InStr 返回找到匹配字串的位置
  • 如果 start > Len(string1) - InStr 返回 0

提示:請參閱 InStrRev 函數。

語法

InStr([start,]string1,string2[,compare])

參數 描述
start 可選。規定每次搜索的起始位置。默認的搜索起始位置是第一個字元(1)。如果已規定 compare 參數,則必須有此參數。
string1 必需。需要被搜索的字串。
string2 必需。需要搜索的字串運算式。
compare 可選。規定要使用的字串比較類型。默認是 0。

可採用下列的值:

  • 0 = vbBinaryCompare - 執行二進位比較
  • 1 = vbTextCompare - 執行文本比較

實例

實例 1

<script type="text/vbscript">

txt="This is a beautiful day!"
document.write(InStr(txt,"beautiful"))

</script>

以上實例輸出結果:

11


實例 2

查找字母 "i",採用不同的起始位置:

<script type="text/vbscript">

txt="This is a beautiful day!"
document.write(InStr(1,txt,"i") & "<br />")
document.write(InStr(7,txt,"i") & "<br />")

</script>

以上實例輸出結果:

3
16


實例 3

查找字母 "t",採用文本和二進位比較:

<script type="text/vbscript">

txt="This is a beautiful day!"
document.write(InStr(1,txt,"t",1) & "<br />")
document.write(InStr(1,txt,"t",0) & "<br />")

</script>

以上實例輸出結果:

1
15


VBScript 參考手冊 完整的 VBScript 參考手冊