VBScript InStr Function

Advertisements


InStr

The InStr Function returns the first occurence of one string within another string. The search happens from left to right.

Syntax :

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

Description :

  • Start, an Optional Parameter. Specifies the starting position for the search. The search begins at the first position from left to right.

  • String1, a Required Parameter. String to be searched.

  • String2, a Required Parameter. String against which String1 is searched.

  • Compare, an Optional Parameter. Specifies the String Comparison to be used. It can take the below mentioned values:

    • 0 = vbBinaryCompare - Performs Binary Comparison(Default)

    • 1 = vbTextCompare - Performs Text Comparison

Example :

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

var="Microsoft VBScript"
document.write("Line 1 : " & InStr(1,var,"s") & "<br />")
document.write("Line 2 : " & InStr(7,var,"s") & "<br />")
document.write("Line 3 : " & InStr(1,var,"f",1) & "<br />")
document.write("Line 4 : " & InStr(1,var,"t",0) & "<br />")
document.write("Line 5 : " & InStr(1,var,"i") & "<br />")
document.write("Line 6 : " & InStr(7,var,"i") & "<br />")
document.write("Line 7 : " & InStr(var,"VB"))

</script>
</body>
</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

Line 1 : 6
Line 2 : 0
Line 3 : 8
Line 4 : 9
Line 5 : 2
Line 6 : 16
Line 7 : 11


Advertisements
Advertisements