InStr Function

Tips >>ASP Functions


The InStr function returns the position of the first occurrence of one string within another. The Function will return the position of the first occurence of String To Find within String To Search. The Function will return 0 if String To Find is not found. Gives the position of the first occurrence of one string within another. The InStr function returns the numeric position of the first occurrence of a specified substring within a specified string when starting from the beginning (left end) of the string. You can have the search for the substring be sensitive to the case (upper versus lower), or not. The default is to be case sensitive (binary comparison). An output of zero indicates no match.

The Syntax of the InStr function is :-

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

Its Description is :-

Parameter Description
start Optional. Specifies the starting position for each search. The search begins at the first character position by default. This parameter is required if compare is specified
string1 Required. The string to be searched
string2 Required. The string expression to search for
compare

Optional. Specifies the string comparison to use. Default is 0
Can have one of the following values:

  • 0 = vbBinaryCompare - Perform a binary comparison
  • 1 = vbTextCompare - Perform a textual comparison

The Compare Argument can have the following values :-

Constant Value Description
vbBinaryCompare 0 Perform a binary comparison.
vbTextCompare 1 Perform a textual comparison.
vbDatabaseCompare 2 Perform a comparison based upon information contained in the database where the comparison is to be performed.

The Instr function returns the following values :-

If Instr returns
string1 is zero-length 0
string1 is Null Null
string2 is zero-length start
string2 is Null Null
string2 is not found 0
string2 is found within string1 Position at which match is found
start > Len(string2) 0

Example#1 :-

Code :-
dim txt,pos
txt="This is a beautiful day!"
pos=InStr(txt,"his")
document.write(pos)
Output :-
2

Example#2 :-

Code :-
dim txt,pos
txt="This is a beautiful day!"
'A textual comparison starting at position 4
pos=InStr(4,txt,"is",1)
document.write(pos)
Output :-
6

Example#3 :-

Code :-
dim txt,pos
txt="This is a beautiful day!"
'A binary comparison starting at position 1
pos=InStr(1,txt,"B",0)
document.write(pos)
Output :-

0

Example#4 :-

Code :-
<%
String1="It's taken me 4 years of work"
String2="t"
Location= Instr(String1,String2)
response.write Location
%>
Output :-

2

Note :- you can set the starting position for each search. If omitted, search begins at the first character position.


Example#5 :-

Code :-
<%
String1="It's taken me 4 years of work"
String2="t"
Location= Instr(3,String1,String2)
response.write Location
%>
Output :-
6

 

 
News / Updates New version of cheque printing software is up. Can configure date in box. http://VirtualSplat.com/cp6.exe
E-mail : sales at virtualsplat dot com
Phone : +91 022 28067978