VBScript InStr 函数

定义和用法

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])
Parameter Description
start Optional. Specifies the starting position for each search. The default is the first character. If the compare parameter is specified, this parameter must also be specified.
string1 Required. The string to be searched for.
string2 Required. The string to be searched.
compare

Required. Specifies the type of string comparison to use. The default is 0. The following values can be used:

  • 0 = vbBinaryCompare - Execute binary comparison.
  • 1 = vbTextCompare - Execute text comparison.

Esimerkki

Example 1

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

Tulo:

2

Example 2

dim txt,pos
txt="This is a beautiful day!"
A textual comparison starting at position 4
pos=InStr(4,txt,"is",1)
document.write(pos)

Tulo:

6

Example 3

dim txt,pos
txt="This is a beautiful day!"
A binary comparison starting at position 1
pos=InStr(1,txt,"B",0)
document.write(pos)

Tulo:

0