Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Strings.InStr Method (Int32, 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)

public static int InStr(
	int Start,
	string String1,
	string String2,
	CompareMethod Compare


Type: System.Int32
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.
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
If String1 is zero length or Nothing, InStr returns 0. If String2 is zero length or Nothing, InStr returns start. If String2 is not found, InStr returns 0. If String2 is found within String1, InStr returns position where match begins. If Start > String2, InStr returns 0.

Typically, the InStr function is used when parsing strings.

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


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2015 Microsoft