Strings.InStr Yöntem

Tanım

Bir dizenin diğeri içindeki ilk örneğinin başlangıç konumunu belirten bir tamsayı döndürür.Returns an integer specifying the start position of the first occurrence of one string within another. Bir eşleşme bulunursa tamsayı tek tabanlı bir dizindir.The integer is a one-based index if a match is found. Hiçbir eşleşme bulunmazsa, işlev sıfır döndürür.If no match is found, the function returns zero.

Aşırı Yüklemeler

InStr(Int32, String, String, CompareMethod)

Bir dizenin diğeri içindeki ilk örneğinin başlangıç konumunu belirten bir tamsayı döndürür.Returns an integer specifying the start position of the first occurrence of one string within another.

InStr(String, String, CompareMethod)

Bir dizenin diğeri içindeki ilk örneğinin başlangıç konumunu belirten bir tamsayı döndürür.Returns an integer specifying the start position of the first occurrence of one string within another.

InStr(Int32, String, String, CompareMethod)

Bir dizenin diğeri içindeki ilk örneğinin başlangıç konumunu belirten bir tamsayı döndürür.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

Parametreler

StartStartPos
Int32

İsteğe bağlı.Optional. Her arama için başlangıç konumunu ayarlayan sayısal ifade.Numeric expression that sets the starting position for each search. Atlanırsa, arama ilk karakter konumunda başlar.If omitted, search begins at the first character position. Başlangıç dizini 1 tabanlıdır.The start index is 1-based.

String1
String

Gereklidir.Required. String aranmakta olan ifade.String expression being searched.

String2
String

Gereklidir.Required. String Aranan ifade.String expression sought.

Compare
CompareMethod

İsteğe bağlı.Optional. Dize karşılaştırma türünü belirtir.Specifies the type of string comparison. CompareAtlanırsa, Option Compare ayar karşılaştırma türünü belirler.If Compare is omitted, the Option Compare setting determines the type of comparison.

Döndürülenler

Int32
EğerIfInStr dönüşlerInStr returns
String1 sıfır uzunluğunda veya Nothing String1 is zero length or Nothing00
String2 sıfır uzunluğunda veya Nothing String2 is zero length or Nothing start
String2 bulunamadı String2 is not found00
String2 içinde bulunur String1 String2 is found within String1 Eşleşmenin başladığı konumPosition where match begins
Start>uzunluğuString1 Start > length of String100

Özel durumlar

Start<1.Start < 1.

Örnekler

Bu örnek, bir InStr dizenin diğeri içindeki ilk oluşumunun konumunu döndürmek için işlevini kullanır.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")

Açıklamalar

Genellikle, InStr dizeler ayrıştırılırken işlev kullanılır.Typically, the InStr function is used when parsing strings.

Not

InStrBVisual Basic önceki sürümlerindeki işlev karakter konumu yerine bir dizi bayt döndürür.The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. Esas olarak çift baytlı karakter kümesi (DBCS) uygulamalarında dize dönüştürmek için kullanılır.It is used primarily for converting strings in double-byte character set (DBCS) applications. Tüm Visual Basic 2005 dizeleri Unicode ve InStrB artık desteklenmiyor.All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

CompareBağımsız değişken ayarları şunlardır:The Compare argument settings are:

SabitConstant DeğerValue AçıklamaDescription
Binary 00 İkili karşılaştırma yaparPerforms a binary comparison
Text 11 Metin karşılaştırması gerçekleştirirPerforms a text comparison

Ayrıca bkz.

Şunlara uygulanır

InStr(String, String, CompareMethod)

Bir dizenin diğeri içindeki ilk örneğinin başlangıç konumunu belirten bir tamsayı döndürür.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

Parametreler

String1
String

Gereklidir.Required. String aranmakta olan ifade.String expression being searched.

String2
String

Gereklidir.Required. String Aranan ifade.String expression sought.

Compare
CompareMethod

İsteğe bağlı.Optional. Dize karşılaştırma türünü belirtir.Specifies the type of string comparison. CompareAtlanırsa, Option Compare ayar karşılaştırma türünü belirler.If Compare is omitted, the Option Compare setting determines the type of comparison.

Döndürülenler

Int32
EğerIfInStr dönüşlerInStr returns
String1 sıfır uzunluğunda veya Nothing String1 is zero length or Nothing00
String2 sıfır uzunluğunda veya Nothing String2 is zero length or NothingArama için başlangıç konumu, varsayılan olarak ilk karakter konumu.The starting position for the search, which defaults to the first character position.
String2 bulunamadı String2 is not found00
String2 içinde bulunur String1 String2 is found within String1Eşleşmenin başladığı konum.Position where match begins.

Örnekler

Bu örnek, bir InStr dizenin diğeri içindeki ilk oluşumunun konumunu döndürmek için işlevini kullanır.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")

Açıklamalar

Genellikle, InStr dizeler ayrıştırılırken işlev kullanılır.Typically, the InStr function is used when parsing strings.

Not

InStrBVisual Basic önceki sürümlerindeki işlev karakter konumu yerine bir dizi bayt döndürür.The InStrB function in previous versions of Visual Basic returns a number of bytes rather than a character position. Esas olarak çift baytlı karakter kümesi (DBCS) uygulamalarında dize dönüştürmek için kullanılır.It is used primarily for converting strings in double-byte character set (DBCS) applications. Tüm Visual Basic 2005 dizeleri Unicode ve InStrB artık desteklenmiyor.All Visual Basic 2005 strings are in Unicode, and InStrB is no longer supported.

CompareBağımsız değişken ayarları şunlardır:The Compare argument settings are:

SabitConstant DeğerValue AçıklamaDescription
Binary 00 İkili karşılaştırma yaparPerforms a binary comparison
Text 11 Metin karşılaştırması gerçekleştirirPerforms a text comparison

Ayrıca bkz.

Şunlara uygulanır