Finding the Presence of a string inside another string by InStr functionWe 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.
InStr(start, string_var, string_part, comparetype)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
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.
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).
|Welcome to Plus2net.com ASP tutorials||asp||0||0|
|Welcome to Plus2net.com ASP tutorials||asp||1||25|
|Welcome to Plus2net.com ASP tutorials||ASP||0||25|
|Welcome to Plus2net.com ASP tutorials||ASP||1||25|
|Welcome to Plus2net.com ASP tutorials||0||1|
|Welcome to Plus2net.com ASP tutorials||1||1|
Be the first to post comment on this article :
ASP functions to handle string|
Adding two or more strings in ASP
Getting the size or length of the string by using len() function
Removing blank space before and after a string using trim function
Changing lower to upper or upper to lower case of text present inside a string
Reversing a string variable by using StrReverse function
Compare two strings in different ways by using StrComp function
Collecting parts from Left , right and middle areas of a string
Checking matching of a string inside another string using InStr function
Searching and replacing strings inside another string using Replace function
Creating arrays by splitting string using split() function
Joining elements of an array to create a string variable
Line Break by replacing text line break to html line break using Replace function