Finding the Presence of a string inside another string by InStr function

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.

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. ( is same as )

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 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.

Welcome to ASP tutorialsasp00
Welcome to ASP tutorialsasp125
Welcome to ASP tutorialsASP025
Welcome to ASP tutorialsASP125
Welcome to ASP tutorials01
Welcome to ASP tutorials11

Be the first to post comment on this article :

Further readings
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

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 All rights reserved worldwide Privacy Policy Disclaimer