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])
Parameters Beschrijving
start Optioneel. Bepaalt de startpositie van elke zoekopdracht. Standaard is de startpositie de eerste character. Als de compare parameter is gedefinieerd, moet deze parameter ook worden gedefinieerd.
string1 Verplicht. De string die moet worden gezocht.
string2 Verplicht. De te zoeken string.
compare

Verplicht. Bepaalt het te gebruiken type stringcomparatie. Standaard is 0. Kan de volgende waarden aannemen:

  • 0 = vbBinaryCompare - Voert een binaire vergelijking uit.
  • 1 = vbTextCompare - Voert een tekstuele vergelijking uit.

Voorbeeld

Voorbeeld 1

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

Uitvoer:

2

Voorbeeld 2

dim txt,pos
txt="This is a beautiful day!"
'Een tekstuele vergelijking die begint op positie 4'
pos=InStr(4,txt,"is",1)
document.write(pos)

Uitvoer:

6

Voorbeeld 3

dim txt,pos
txt="This is a beautiful day!"
'Een binaire vergelijking die begint op positie 1'
pos=InStr(1,txt,"B",0)
document.write(pos)

Uitvoer:

0