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)

public static int InStr (
	string String1,
	string String2,
	[OptionalAttribute] CompareMethod Compare
)
public static int InStr (
	String String1, 
	String String2, 
	/** @attribute OptionalAttribute() */ CompareMethod Compare
)
public static function InStr (
	String1 : String, 
	String2 : String, 
	Compare : CompareMethod
) : int

Parameters

String1

Required. String expression being searched.

String2

Required. String expression sought.

Compare

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

Return Value

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.

For more detailed information, see the Visual Basic topic InStr Function (Visual Basic).

Typically, the InStr function is used when parsing strings.

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft