Find Text with InStr() in Classic ASP

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() syntax and parameters

InStr() accepts the text to search, the text to find, and optional controls for starting position and comparison type:

InStr([start,] string1, string2 [, compare])
ParameterMeaning
startOptional 1-based position where searching begins. Required if compare is provided.
string1The complete text you want to search.
string2The text you are searching for.
compareOptional 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.

Case-sensitive and case-insensitive searches

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 stringSearch textComparisonResult
Welcome to Plus2net.com ASP tutorialsaspBinary0
Welcome to Plus2net.com ASP tutorialsaspText25
Welcome to Plus2net.com ASP tutorialsASPBinary25
Welcome to Plus2net.com ASP tutorialsASPText25
Welcome to Plus2net.com ASP tutorials(empty string)Binary1

These rows preserve the comparisons shown in the original tutorial. For matching entire strings instead of searching for a substring, see StrComp().

Search from a particular starting position

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.

Check whether a substring exists

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.

Find every occurrence of a word

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.

Extract characters after a delimiter

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.

Search text submitted from a form

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.

Empty strings, Null values and return rules

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.

ConditionResult
Text to search is empty0
Search term is emptyStarting position, normally 1
Term is not found0
Either text argument is NullNull
Start position is beyond the available text0
<%
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.

Common InStr() errors and troubleshooting

ProblemCause and solution
Expected match returns 0Check spelling and casing. Use vbTextCompare if you intend a case-insensitive search.
Match starts one position later than expectedVBScript InStr() returns positions starting from 1, not 0.
Compare parameter causes an argument errorInclude the starting position, e.g. InStr(1, text, term, vbTextCompare).
Searching for an empty string returns 1An empty search term returns the starting position. Check that the term is not empty before searching.
Search against database field behaves unexpectedlyCheck for Null using IsNull() before testing the result numerically.
Need the final occurrence instead of the firstUse InStrRev() where appropriate, or repeat a forward search.

Frequently asked questions

Does InStr() start counting from 0 or 1?

It starts at 1. A match in the first character returns 1.

Does InStr() return True or False?

No. It returns the matching position (a positive number), 0 when not found, or Null if an input string is Null.

How do I make InStr() case-insensitive?

Use vbTextCompare with the four-argument form: InStr(1, sourceText, searchText, vbTextCompare).

How can I find the last occurrence of a character?

Use InStrRev() to search backward from the end of a string.

Can I use InStr() to validate email addresses?

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.

Back to top ↑


ASP Home






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