Strings.InStr Metoda

Definice

Vrací celé číslo určující počáteční pozici prvního výskytu jednoho řetězce v rámci druhého.Returns an integer specifying the start position of the first occurrence of one string within another. Celé číslo je index založený na jednom, pokud je nalezena shoda.The integer is a one-based index if a match is found. Pokud není nalezena žádná shoda, vrátí funkce hodnotu nula.If no match is found, the function returns zero.

Přetížení

InStr(Int32, String, String, CompareMethod)

Vrací celé číslo určující počáteční pozici prvního výskytu jednoho řetězce v rámci druhého.Returns an integer specifying the start position of the first occurrence of one string within another.

InStr(String, String, CompareMethod)

Vrací celé číslo určující počáteční pozici prvního výskytu jednoho řetězce v rámci druhého.Returns an integer specifying the start position of the first occurrence of one string within another.

InStr(Int32, String, String, CompareMethod)

Vrací celé číslo určující počáteční pozici prvního výskytu jednoho řetězce v rámci druhého.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 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

Parametry

StartStartPos
Int32

Nepovinný parametr.Optional. Číselný výraz, který nastaví počáteční pozici pro každé hledání.Numeric expression that sets the starting position for each search. Pokud tento parametr vynecháte, hledání začne na první pozici znaku.If omitted, search begins at the first character position. Počáteční index je založen na 1.The start index is 1-based.

String1
String

Povinná hodnota.Required. String hledaný výraz.String expression being searched.

String2
String

Povinná hodnota.Required. String byl vyžádán výraz.String expression sought.

Compare
CompareMethod

Nepovinný parametr.Optional. Určuje typ porovnání řetězců.Specifies the type of string comparison. Pokud Compare je tento parametr vynechán, Option Compare nastavení určuje typ porovnání.If Compare is omitted, the Option Compare setting determines the type of comparison.

Návraty

Int32
Pokud uživatelIfFunkce InStr vrátíInStr returns
String1 má nulovou délku nebo Nothing String1 is zero length or Nothing00
String2 má nulovou délku nebo Nothing String2 is zero length or Nothing start
String2 nebyl nalezen String2 is not found00
String2 byl nalezen v rámci String1 String2 is found within String1 Pozice, kde začíná shodaPosition where match begins
Start>DélkaString1 Start > length of String100

Výjimky

Start<1.Start < 1.

Příklady

Tento příklad používá InStr funkci k vrácení pozice prvního výskytu jednoho řetězce v jiném.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")

Poznámky

Tato InStr funkce se obvykle používá při analýze řetězců.Typically, the InStr function is used when parsing strings.

Poznámka

InStrBFunkce v předchozích verzích Visual Basic vrátí počet bajtů namísto pozice znaku.The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. Používá se především pro převod řetězců v aplikacích dvoubajtové znakové sady (DBCS).It is used primarily for converting strings in double-byte character set (DBCS) applications. Všechny řetězce Visual Basic 2005 jsou v kódování Unicode a InStrB již nejsou podporovány.All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

CompareNastavení argumentů jsou:The Compare argument settings are:

KonstantaConstant HodnotaValue PopisDescription
Binary 00 Provádí binární porovnání.Performs a binary comparison
Text 11 Provede porovnání textu.Performs a text comparison

Viz také

Platí pro

InStr(String, String, CompareMethod)

Vrací celé číslo určující počáteční pozici prvního výskytu jednoho řetězce v rámci druhého.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

Parametry

String1
String

Povinná hodnota.Required. String hledaný výraz.String expression being searched.

String2
String

Povinná hodnota.Required. String byl vyžádán výraz.String expression sought.

Compare
CompareMethod

Nepovinný parametr.Optional. Určuje typ porovnání řetězců.Specifies the type of string comparison. Pokud Compare je tento parametr vynechán, Option Compare nastavení určuje typ porovnání.If Compare is omitted, the Option Compare setting determines the type of comparison.

Návraty

Int32
Pokud uživatelIfFunkce InStr vrátíInStr returns
String1 má nulovou délku nebo Nothing String1 is zero length or Nothing00
String2 má nulovou délku nebo Nothing String2 is zero length or NothingPočáteční pozice pro hledání, která je výchozím nastavením první pozice znaku.The starting position for the search, which defaults to the first character position.
String2 nebyl nalezen String2 is not found00
String2 byl nalezen v rámci String1 String2 is found within String1Pozice, kde začíná shoda.Position where match begins.

Příklady

Tento příklad používá InStr funkci k vrácení pozice prvního výskytu jednoho řetězce v jiném.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")

Poznámky

Tato InStr funkce se obvykle používá při analýze řetězců.Typically, the InStr function is used when parsing strings.

Poznámka

InStrBFunkce v předchozích verzích Visual Basic vrátí počet bajtů namísto pozice znaku.The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. Používá se především pro převod řetězců v aplikacích dvoubajtové znakové sady (DBCS).It is used primarily for converting strings in double-byte character set (DBCS) applications. Všechny řetězce Visual Basic 2005 jsou v kódování Unicode a InStrB již nejsou podporovány.All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

CompareNastavení argumentů jsou:The Compare argument settings are:

KonstantaConstant HodnotaValue PopisDescription
Binary 00 Provádí binární porovnání.Performs a binary comparison
Text 11 Provede porovnání textu.Performs a text comparison

Viz také

Platí pro