Strings.InStr 方法

定义

返回一个整数,该整数指定一个字符串在另一个字符串中的第一个匹配项的起始位置。Returns an integer specifying the start position of the first occurrence of one string within another. 如果找到了匹配项,则该整数是从 1 开始的索引。The integer is a one-based index if a match is found. 如果未找到匹配项,则函数返回零。If no match is found, the function returns zero.

重载

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)

返回一个整数,该整数指定一个字符串在另一个字符串中的第一个匹配项的起始位置。Returns an integer specifying the start position of the first occurrence of one string within another.

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
Public Function InStr (Start As Integer, String1 As String, String2 As String, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As Integer

参数

Start
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 表达式。String expression being searched.

String2
String

必需。Required. 查找到的 String 表达式。String expression sought.

Compare
CompareMethod

可选。Optional. 指定字符串比较的类型。Specifies the type of string comparison. 如果省略 Compare,则由 Option Compare 设置确定比较类型。If Compare is omitted, the Option Compare setting determines the type of comparison.

返回

如果If InStr 返回InStr returns
String1 的长度为零或 Nothing String1 is zero length or Nothing 00
String2 的长度为零或 Nothing String2 is zero length or Nothing start
未找到 String2 String2 is not found 00
String2 中找到 String1 String2 is found within String1 匹配开始的位置Position where match begins
Start > 长度 String1 Start > length of String1 00
.

异常

Start < 1。Start < 1.

示例

本例使用 InStr 函数返回一个字符串在另一个字符串中的第一个匹配项的位置。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")

注解

通常,在分析字符串时使用 InStr 函数。Typically, the InStr function is used when parsing strings.

备注

Visual Basic 以前版本中的 InStrB 函数返回的字节数而不是字符位置。The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. 它主要用于在双字节字符集(DBCS)应用程序中转换字符串。It is used primarily for converting strings in double-byte character set (DBCS) applications. 所有 Visual Basic 2005 字符串均采用 Unicode 格式,InStrB 不再受支持。All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

Compare 参数的设置是:The Compare argument settings are:

返回的常量Constant “值”Value 描述Description
Binary 00 执行二进制比较Performs a binary comparison
Text 11 执行文本比较Performs a text comparison

另请参阅

InStr(String, String, CompareMethod)

返回一个整数,该整数指定一个字符串在另一个字符串中的第一个匹配项的起始位置。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);
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

参数

String1
String

必需。Required. 正在搜索的 String 表达式。String expression being searched.

String2
String

必需。Required. 查找到的 String 表达式。String expression sought.

Compare
CompareMethod

可选。Optional. 指定字符串比较的类型。Specifies the type of string comparison. 如果省略 Compare,则由 Option Compare 设置确定比较类型。If Compare is omitted, the Option Compare setting determines the type of comparison.

返回

如果If InStr 返回InStr returns
String1 的长度为零或 Nothing String1 is zero length or Nothing 00
String2 的长度为零或 Nothing String2 is zero length or Nothing 搜索的开始位置,该位置默认为第一个字符位置。The starting position for the search, which defaults to the first character position.
未找到 String2 String2 is not found 00
String2 中找到 String1 String2 is found within String1 匹配开始的位置Position where match begins
.

示例

本例使用 InStr 函数返回一个字符串在另一个字符串中的第一个匹配项的位置。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")

注解

通常,在分析字符串时使用 InStr 函数。Typically, the InStr function is used when parsing strings.

备注

Visual Basic 以前版本中的 InStrB 函数返回的字节数而不是字符位置。The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. 它主要用于在双字节字符集(DBCS)应用程序中转换字符串。It is used primarily for converting strings in double-byte character set (DBCS) applications. 所有 Visual Basic 2005 字符串均采用 Unicode 格式,InStrB 不再受支持。All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

Compare 参数的设置是:The Compare argument settings are:

返回的常量Constant “值”Value 描述Description
Binary 00 执行二进制比较Performs a binary comparison
Text 11 执行文本比较Performs a text comparison

另请参阅

适用于