Strings.InStr Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an integer specifying the start position of the first occurrence of one string within another. The integer is a one-based index if a match is found. If no match is found, the function returns zero.
Overloads
InStr(Int32, String, String, CompareMethod) |
Returns an integer specifying the start position of the first occurrence of one string within another. |
InStr(String, String, CompareMethod) |
Returns an integer specifying the start position of the first occurrence of one string within another. |
InStr(Int32, String, String, CompareMethod)
- Source:
- Strings.vb
- Source:
- Strings.vb
- Source:
- Strings.vb
Returns an integer specifying the start position of the first occurrence of one string within another.
public static int InStr (int StartPos, string? String1, string? String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
public static int InStr (int Start, string? String1, string? String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
public static int InStr (int StartPos, string String1, string String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
public static int InStr (int Start, string String1, string String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
static member InStr : int * string * string * Microsoft.VisualBasic.CompareMethod -> int
static member InStr : int * string * string * Microsoft.VisualBasic.CompareMethod -> int
Public Function InStr (StartPos As Integer, String1 As String, String2 As String, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As Integer
Public Function InStr (Start As Integer, String1 As String, String2 As String, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As Integer
Parameters
- StartStartPos
- 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
- String
Required. String
expression being searched.
- String2
- String
Required. String
expression sought.
- Compare
- CompareMethod
Optional. Specifies the type of string comparison. If Compare
is omitted, the Option Compare
setting determines the type of comparison.
Returns
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 |
Exceptions
Start
< 1.
Examples
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")
Remarks
Typically, the InStr
function is used when parsing strings.
Note
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 |
See also
- InStrRev(String, String, Int32, CompareMethod)
- StrComp(String, String, CompareMethod)
- ArgumentException
- Strings in Visual Basic
- Introduction to Strings in Visual Basic
- Option Compare Statement
Applies to
InStr(String, String, CompareMethod)
- Source:
- Strings.vb
- Source:
- Strings.vb
- Source:
- Strings.vb
Returns an integer specifying the start position of the first occurrence of one string within another.
public static int InStr (string? String1, string? String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
public static int InStr (string String1, string String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
static member InStr : string * string * Microsoft.VisualBasic.CompareMethod -> int
Public Function InStr (String1 As String, String2 As String, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As Integer
Parameters
- String1
- String
Required. String
expression being searched.
- String2
- String
Required. String
expression sought.
- Compare
- CompareMethod
Optional. Specifies the type of string comparison. If Compare
is omitted, the Option Compare
setting determines the type of comparison.
Returns
If | InStr returns |
---|---|
String1 is zero length or Nothing | 0 |
String2 is zero length or Nothing | The starting position for the search, which defaults to the first character position. |
String2 is not found | 0 |
String2 is found within String1 | Position where match begins. |
Examples
This example uses the InStr
function to return the position of the first occurrence of one string within another. In the first example, the search starts from the fourth character and returns the next lower case "p" because CompareMethod
is Text
and therefore case insensitive. The position is always relative to the beginning of the string, regardless of the start position.
' 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")
Remarks
Typically, the InStr
function is used when parsing strings.
Note
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 |
See also
- InStrRev(String, String, Int32, CompareMethod)
- StrComp(String, String, CompareMethod)
- ArgumentException
- Strings in Visual Basic
- Introduction to Strings in Visual Basic
- Option Compare Statement