Latest web development tutorials

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參考手冊