Latest web development tutorials

VBScript InStr Function

VBScript Reference Complete VBScript Reference

InStr function returns the position of the first occurrence of a string in another string.

InStr function returns the following values:

  • If string1 is "" - InStr returns 0
  • If string1 is Null - InStr returns Null
  • If string2 is "" - InStr returns start
  • If string2 is Null - InStr returns Null
  • If string2 is not found - InStr returns 0
  • If string2 is found within string1 in - InStr returns to find the matching string location
  • If start> Len (string1) - InStr returns 0

Tip: See InStrRev function.

grammar

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

参数 描述
start 可选。规定每次搜索的起始位置。默认的搜索起始位置是第一个字符(1)。如果已规定 compare 参数,则必须有此参数。
string1 必需。需要被搜索的字符串。
string2 必需。需要搜索的字符串表达式。
compare 可选。规定要使用的字符串比较类型。默认是 0。

可采用下列的值:

  • 0 = vbBinaryCompare - 执行二进制比较
  • 1 = vbTextCompare - 执行文本比较

Examples

Example 1

<script type="text/vbscript">

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

</script>

Examples of the above output:

11

try it"

Example 2

Find the letter "i", with different start position:

<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>

Examples of the above output:

3
16

try it"

Example 3

Find the letter "t", using both text and binary comparison:

<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>

Examples of the above output:

1
15

try it"

VBScript Reference Complete VBScript Reference