Finding the Presence of a string inside another string by InStr function
Dim string_var,string_part
string_var="Welcome to Plus2net.com ASP tutorials"
string_part= "ASP"
Response.Write InStr(string_var,string_part)
The output of the above code is 25.
We can check the presence or matching of a string inside another string by using InStr function of VBScript used in ASP. The matching can be checked in various ways by adding optional arguments to the function. Here is the syntax of InStr function.
In the above syntax start and comparetype are two optional arguments. string_var is the main string variable or the string and inside this string_var the presence of string_part is checked or compared. The other optional argument start specifies where to start the search within string_var
optional argument comparetype
The optional argument comparetype can take value 0 or 1. By default this value is set to 0.
If the value of comparetype is set to 0 then it is a binary comparison, so it is case sensitive and as a result lower and upper case letters are treated differently.
If the value of comparetype is set to 1 then this became textual comparison so it is case insensitive and as a result lower and upper case text are treated as same. ( www.plus2net.com is same as www.Plus2net.com )
This function returns the exact position of the occurrence of the checked string if found and returns 0 if string_part is not found inside string_var. It returns NULL if either string is NULL. The starting point is returned if any of the two strings is empty. The table at the end of this page gives different return values for the InStr function with different argument values.
Here is the basic code for InStr function.
Dim string_var,string_part
string_var="Welcome to Plus2net.com ASP tutorials"
string_part= "ASP"
Response.Write InStr(string_var,string_part)
The output of the above code is 25. So the word ASP location is at 25th position from left inside the main string (string_var).
If we change different values of string_var and string_part with two values of comparetype the output we will get is given here in a table.