Export (0) Print
Expand All

Strings::InStr Method (String, String, CompareMethod)

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

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

static int InStr(
	String^ String1, 
	String^ String2, 
	CompareMethod Compare


Type: System::String

Required. String expression being searched.

Type: System::String

Required. String expression sought.

Type: Microsoft.VisualBasic::CompareMethod

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

Return Value

Type: System::Int32


InStr returns

String1 is zero length or Nothing


String2 is zero length or Nothing

The starting position for the search, which defaults to the first character position.

String2 is not found


String2 is found within String1

Position where match begins

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.

The Compare argument settings are:






Performs a binary comparison



Performs a text comparison

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")

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft