Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 = CompareMethod::Binary
)

Parameters

Start
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.

String1
Type: System::String^

Required. String expression being searched.

String2
Type: System::String^

Required. String expression sought.

Compare
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

InStr returns

String1 is zero length or Nothing

0

String2 is zero length or Nothing

start

String2 is not found

0

String2 is found within String1

Position where match begins

Start > length of String1

0

Exception Condition
ArgumentException

Start < 1.

Typically, the InStr function is used when parsing strings.

System_CAPS_noteNote

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:

Constant

Value

Description

Binary

0

Performs a binary comparison

Text

1

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
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show:
© 2017 Microsoft