In Classic ASP, InStr() finds the position of text inside another string. The result is a 1-based character position: the first character is position 1. If the search text is not found, the function returns 0.
Here is a complete VBScript example you can run in an .asp page on IIS:
<%
Dim string_var, string_part
string_var = "Welcome to Plus2net.com ASP tutorials"
string_part = "ASP"
Response.Write "Position: " & InStr(1, string_var, string_part, vbBinaryCompare)
%>
Output: Position: 25. The letters ASP begin at the 25th character in the sample string.
InStr() accepts the text to search, the text to find, and optional controls for starting position and comparison type:
InStr([start,] string1, string2 [, compare])
| Parameter | Meaning |
|---|---|
start | Optional 1-based position where searching begins. Required if compare is provided. |
string1 | The complete text you want to search. |
string2 | The text you are searching for. |
compare | Optional comparison mode: vbBinaryCompare (0) or vbTextCompare (1). |
When providing the comparison argument, include the starting position. For example, use InStr(1, text, "ASP", vbTextCompare), not a three-argument call that omits start.
The original Plus2Net example searches for ASP in a longer string. Without an explicit starting position, InStr() begins at character 1.
<%
Dim string_var, string_part
string_var="Welcome to Plus2net.com ASP tutorials"
string_part="ASP"
Response.Write InStr(string_var, string_part)
%>
Output: 25.
Use vbBinaryCompare for binary comparison and vbTextCompare for text comparison that ignores case differences, such as ASP versus asp. Specify the mode explicitly when it matters.
<%
Dim string_var, string_part
string_var="Welcome to Plus2net.com ASP tutorials"
string_part="ASP"
Response.Write InStr(1, string_var, string_part, vbBinaryCompare)
%>
Output: 25. This is the original explicit-search example.
<%
Dim sentence
sentence = "Welcome to Plus2net.com ASP tutorials"
Response.Write "Binary: " & InStr(1, sentence, "asp", vbBinaryCompare) & "<br>"
Response.Write "Text: " & InStr(1, sentence, "asp", vbTextCompare)
%>
Binary: 0
Text: 25
| Main string | Search text | Comparison | Result |
|---|---|---|---|
| Welcome to Plus2net.com ASP tutorials | asp | Binary | 0 |
| Welcome to Plus2net.com ASP tutorials | asp | Text | 25 |
| Welcome to Plus2net.com ASP tutorials | ASP | Binary | 25 |
| Welcome to Plus2net.com ASP tutorials | ASP | Text | 25 |
| Welcome to Plus2net.com ASP tutorials | (empty string) | Binary | 1 |
These rows preserve the comparisons shown in the original tutorial. For matching entire strings instead of searching for a substring, see StrComp().
The start parameter is useful when the search term appears more than once. To find the next occurrence, start searching after the previous match.
<%
Dim sentence, firstPos, secondPos
sentence = "ASP guides and ASP examples"
firstPos = InStr(1, sentence, "ASP", vbBinaryCompare)
secondPos = InStr(firstPos + 1, sentence, "ASP", vbBinaryCompare)
Response.Write "First: " & firstPos & "<br>Second: " & secondPos
%>
First: 1
Second: 16
Search positions count from 1, not 0, which is also important when you use Mid() to extract nearby text.
InStr() returns 0 when a search term is absent. Use a numeric comparison such as > 0 when you only need a yes-or-no result.
<%
Dim content, pos
content = "Learn Classic ASP with Plus2net"
pos = InStr(1, content, "Python", vbTextCompare)
If pos > 0 Then
Response.Write "Found at position " & pos
Else
Response.Write "Text not found"
End If
%>
Output: Text not found.
To find several matches, use a loop that starts the next search after the previous match. This example reports non-overlapping matches, including case variations.
<%
Dim sourceText, searchText, foundAt
sourceText = "ASP, asp and ASP"
searchText = "ASP"
foundAt = InStr(1, sourceText, searchText, vbTextCompare)
Do While foundAt > 0
Response.Write "Match at " & foundAt & "<br>"
foundAt = InStr(foundAt + Len(searchText), sourceText, searchText, vbTextCompare)
Loop
%>
Match at 1
Match at 6
Match at 14
For overlapping matches, advance the starting position by 1 instead of Len(searchText). A non-empty search term is important so the loop can progress.
Combine InStr() with Mid() to obtain the portion after a separator. Search for the delimiter first so a missing separator can be handled.
<%
Dim pairText, separatorPos, valueText
pairText = "name=John"
separatorPos = InStr(1, pairText, "=", vbBinaryCompare)
If separatorPos > 0 Then
valueText = Mid(pairText, separatorPos + 1)
Response.Write valueText
Else
Response.Write "Separator not found"
End If
%>
Output: John. This is useful for learning how positions work, but URL query strings should be read with Request.QueryString() rather than manually split because values can be URL-encoded and repeated.
Here is a complete Classic ASP form example. The visitor submits a sentence and a search term; InStr() reports the first match. The result is a number, so no visitor-submitted HTML is displayed.
<form method="post" action="">
<label for="sentence">Sentence</label>
<input type="text" id="sentence" name="sentence" required>
<label for="keyword">Find text</label>
<input type="text" id="keyword" name="keyword" required>
<button type="submit">Find position</button>
</form>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim sentence, keyword, foundAt
sentence = Trim(Request.Form("sentence"))
keyword = Trim(Request.Form("keyword"))
If Len(keyword) > 0 Then
foundAt = InStr(1, sentence, keyword, vbTextCompare)
If foundAt > 0 Then
Response.Write "Found at position " & foundAt
Else
Response.Write "Text not found"
End If
Else
Response.Write "Enter some text to search for."
End If
End If
%>
For real applications, validate maximum input lengths server-side and HTML-encode any visitor-supplied text before displaying it using Server.HTMLEncode(). Learn more about Classic ASP forms and the Request object.
These conditions can change the result of InStr(). Unlike an empty string, Null represents a missing/unknown value, and it propagates to the result of the function.
| Condition | Result |
|---|---|
| Text to search is empty | 0 |
| Search term is empty | Starting position, normally 1 |
| Term is not found | 0 |
Either text argument is Null | Null |
| Start position is beyond the available text | 0 |
<%
Response.Write InStr(1, "ASP", "", vbBinaryCompare) & "<br>"
Response.Write InStr(1, "", "ASP", vbBinaryCompare) & "<br>"
Response.Write InStr(1, "ASP", "Python", vbBinaryCompare)
%>
1
0
0
Before testing InStr() against a nullable database value, use IsNull() to choose a non-null string or skip the search.
<%
Dim databaseText, foundAt
databaseText = Null
If IsNull(databaseText) Then
foundAt = 0
Else
foundAt = InStr(1, databaseText, "ASP", vbTextCompare)
End If
Response.Write foundAt
%>
Output: 0. Avoid comparing a Null result directly to an integer in If statements.
InStr() searches forward and returns a character position. InStrRev() finds the last occurrence when searching backward; this can help find a final period or delimiter in filenames. InStrB() performs a byte-oriented position search, which is different from counting characters.
<%
Dim fileName
fileName = "report.final.csv"
Response.Write "First dot: " & InStr(1, fileName, ".", vbBinaryCompare) & "<br>"
Response.Write "Last dot: " & InStrRev(fileName, ".")
%>
First dot: 7
Last dot: 13
For more extraction examples see Left(), Right() and Mid(). For filesystem paths and extensions on the server, see FileSystemObject methods.
| Problem | Cause and solution |
|---|---|
| Expected match returns 0 | Check spelling and casing. Use vbTextCompare if you intend a case-insensitive search. |
| Match starts one position later than expected | VBScript InStr() returns positions starting from 1, not 0. |
| Compare parameter causes an argument error | Include the starting position, e.g. InStr(1, text, term, vbTextCompare). |
| Searching for an empty string returns 1 | An empty search term returns the starting position. Check that the term is not empty before searching. |
| Search against database field behaves unexpectedly | Check for Null using IsNull() before testing the result numerically. |
| Need the final occurrence instead of the first | Use InStrRev() where appropriate, or repeat a forward search. |
It starts at 1. A match in the first character returns 1.
No. It returns the matching position (a positive number), 0 when not found, or Null if an input string is Null.
Use vbTextCompare with the four-argument form: InStr(1, sourceText, searchText, vbTextCompare).
Use InStrRev() to search backward from the end of a string.
You can check whether a character such as @ appears, but that does not establish a valid email address. Use an appropriate validation approach instead; the Classic ASP regular-expression tutorial introduces pattern-based checks.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.