VBScript
InStr
function
The VBScript InStr function is used to return where one string first appears in another string.
Complete VBScript reference manual
The InStr function returns the position where one string first appears in another string.
The InStr function returns the following value:
-
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 - InStr is found in string1 returns the location where the matching string was found
-
If start sgt; Len (string1) - InStr returns 0
Tip:
See InStrRev function.
Grammar
InStr([start,]string1,string2[,compare])
parameter
|
describe
|
start
|
Optional.Specifies the starting position of each search.The default search start position is the first character (1).This parameter must be available if the Compare parameter has been specified.
|
string1
|
Required.A string that needs to be searched.
|
string2
|
Required.String expressions that need to be searched.
|
compare
|
Optional.The string comparison type to use is specified.The default is 0.
The following values can be used:
-
0 = VBBINARYCOMPARE - Performing binary comparison
-
1 = vbtextcompare - Perform text comparison
|
Instance 1
<script type="text/vbscript">
txt="This is a beautiful day!"
document.write(InStr(txt,"beautiful"))
</script>
The above example output results:
11
Try it out . . .
Instance 2
Look for the letter "i" with a different starting 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>
The above example output results:
3
16
Try it out . . .
Instance 3
Look for the letter "t" with text and binary comparisons:
<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>
The above example output results:
1
15
Try it out . . .
Complete VBScript reference manual