This documentation is archived and is not being maintained.

InStr Function (Visual Basic) 

Returns an integer specifying the start position of the first occurrence of one string within another.

Public Shared Function InStr(_
   ByVal String1 As String, _
   ByVal String2 As String, _
   Optional ByVal Compare As CompareMethod _
) As Integer
' -or-
Public Shared Function InStr(_
   ByVal Start As Integer, _
   ByVal String1 As String, _
   ByVal String2 As String, _
   Optional ByVal Compare As Microsoft.VisualBasic.CompareMethod _
) As Integer



Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. The start index is 1-based.


Required. String expression being searched.


Required. String expression sought.


Optional. Specifies the type of string comparison. If Compare is omitted, the Option Compare setting determines the type of comparison.

The Compare argument settings are:

Constant Value Description



Performs a binary comparison



Performs a text comparison

If InStr returns

String1 is zero length or Nothing


String2 is zero length or Nothing


String2 is not found


String2 is found within String1

Position where match begins

Start > String2


Exception type Error number Condition



Start < 1.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Typically, the InStr function is used when parsing strings.


The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. It is used primarily for converting strings in double-byte character set (DBCS) applications. All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

This example uses the InStr function to return the position of the first occurrence of one string within another.

' String to search in.
Dim SearchString As String = "XXpXXpXXPXXP"
' Search for "P".
Dim SearchChar As String = "P"

Dim TestPos As Integer
' A textual comparison starting at position 4. Returns 6.
TestPos = InStr(4, SearchString, SearchChar, CompareMethod.Text)

' A binary comparison starting at position 1. Returns 9.
TestPos = InStr(1, SearchString, SearchChar, CompareMethod.Binary)

' If Option Compare is not set, or set to Binary, return 9.
' If Option Compare is set to Text, returns 3.
TestPos = InStr(SearchString, SearchChar)

' Returns 0.
TestPos = InStr(1, SearchString, "W")

Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)