Strings.Split(String, String, Int32, CompareMethod) Metoda
Definice
Vrací jednorozměrné pole s nulovým základem obsahující určený počet podřetězců.Returns a zero-based, one-dimensional array containing a specified number of substrings.
public static string[] Split (string? Expression, string? Delimiter = " ", int Limit = -1, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
public static string[] Split (string Expression, string Delimiter = " ", int Limit = -1, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
static member Split : string * string * int * Microsoft.VisualBasic.CompareMethod -> string[]
Public Function Split (Expression As String, Optional Delimiter As String = " ", Optional Limit As Integer = -1, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As String()
Parametry
- Expression
- String
Povinná hodnota.Required. String výraz obsahující podřetězce a oddělovačeString expression containing substrings and delimiters.
- Delimiter
- String
Nepovinný parametr.Optional. Libovolný jeden znak, který slouží k identifikaci podřetězců.Any single character used to identify substring limits. Pokud Delimiter je tento parametr vynechán, předpokládá se, že znak mezery ("") je oddělovačem.If Delimiter is omitted, the space character (" ") is assumed to be the delimiter.
- Limit
- Int32
Nepovinný parametr.Optional. Maximální počet podřetězců, do kterých by měl být vstupní řetězec rozdělen.Maximum number of substrings into which the input string should be split. Výchozí hodnota-1 označuje, že vstupní řetězec by měl být rozdělen na všechny výskyty Delimiter řetězce.The default, -1, indicates that the input string should be split at every occurrence of the Delimiter string.
- Compare
- CompareMethod
Nepovinný parametr.Optional. Číselná hodnota označující porovnání, které se má použít při vyhodnocování podřetězců.Numeric value indicating the comparison to use when evaluating substrings. Hodnoty naleznete v části „Nastavení“.See "Settings" for values.
Návraty
- String[]
String skupin.String array. Pokud Expression je řetězec s nulovou délkou (""), Split vrátí pole s jedním elementem obsahujícím řetězec s nulovou délkou.If Expression is a zero-length string (""), Split returns a single-element array containing a zero-length string. Pokud Delimiter je řetězec s nulovou délkou, nebo pokud se nezobrazí kdekoli v Expression , Split vrátí pole s jedním prvkem obsahující celý Expression řetězec.If Delimiter is a zero-length string, or if it does not appear anywhere in Expression, Split returns a single-element array containing the entire Expression string.
Příklady
Následující příklad ukazuje, jak rozdělit řetězec ve svých prostorech.The following example demonstrates how to split a string at its spaces.
Dim testString As String = "Look at these!"
' Returns an array containing "Look", "at", and "these!".
Dim testArray() As String = Split(testString)
Následující příklad ukazuje, jak rozdělit řetězce s více oddělovači v řádku a vyfiltrovat prázdné řetězce.The following example demonstrates how to split strings with multiple delimiters in a row and filter out the empty strings.
Dim testString As String = "apple pear banana "
Dim testArray() As String = Split(testString)
' testArray holds {"apple", "", "", "", "pear", "banana", "", ""}
Dim lastNonEmpty As Integer = -1
For i As Integer = 0 To testArray.Length - 1
If testArray(i) <> "" Then
lastNonEmpty += 1
testArray(lastNonEmpty) = testArray(i)
End If
Next
ReDim Preserve testArray(lastNonEmpty)
' testArray now holds {"apple", "pear", "banana"}
Poznámky
Ve výchozím nastavení, nebo je Limit -li rovno-1, Split funkce rozdělí vstupní řetězec na všechny výskyty řetězce oddělovače a vrátí podřetězce v poli.By default, or when Limit equals -1, the Split function splits the input string at every occurrence of the delimiter string, and returns the substrings in an array. Pokud Limit je parametr větší než nula, Split funkce rozdělí řetězec na prvních Limit 1 výskyt oddělovače a vrátí pole s výslednými podřetězci.When the Limit parameter is greater than zero, the Split function splits the string at the first Limit-1 occurrences of the delimiter, and returns an array with the resulting substrings. Například Split("a:b:c", ":") vrátí pole {"a", "b", "c"} , zatímco Split("a:b:c", ":", 2) vrací pole {"a", "b:c"} .For example, Split("a:b:c", ":") returns the array {"a", "b", "c"}, while Split("a:b:c", ":", 2) returns the array {"a", "b:c"}.
Když Split funkce nalezne dva oddělovače v řádku nebo oddělovač na začátku nebo konci řetězce, interpretuje je jako ohraničující prázdný řetězec ("").When the Split function encounters two delimiters in a row, or a delimiter at the beginning or end of the string, it interprets them as surrounding an empty string (""). Například Split("xx", "x") vrátí pole obsahující tři prázdné řetězce: jedno od začátku řetězce a první "x", jeden od dvou řetězců "x" a jeden z poslední "x" a konec řetězce.For example, Split("xx", "x") returns the array containing three empty strings: one from between the beginning of the string and the first "x", one from between the two "x" strings, and one from between the last "x" and the end of the string.
Tato tabulka ukazuje, jak volitelné Delimiter Limit parametry, a Compare mohou změnit chování Split funkce.This table demonstrates how the optional Delimiter, Limit, and Compare parameters can change the behavior of the Split function.
| Rozdělené voláníSplit Call | Návratová hodnotaReturn Value |
|---|---|
Split("42, 12, 19") |
{"42,", "12,", "19"}{"42," , "12," , "19"} |
Split("42, 12, 19", ", ") |
{"42", "12", "19"}{"42", "12", "19"} |
Split("42, 12, 19", ", ", 2) |
{"42", "12, 19"}{"42", "12, 19"} |
Split("192.168.0.1", ".") |
{"192", "168", "0", "1"}{"192", "168", "0", "1"} |
Split("Alice and Bob", " AND ") |
{"Alice a Bob"}{"Alice and Bob"} |
Split("Alice and Bob", " AND ", ,CompareMethod.Text) |
{"Alice", "Bob"}{"Alice", "Bob"} |
Split("someone@example.com", "@",1) |
{"someone@example.com"}{"someone@example.com"} |
Split("someone@example.com", "@",2) |
{"někoho", "example.com"}{"someone", "example.com"} |
CompareArgument může obsahovat následující hodnoty.The Compare argument can have the following values.
| KonstantaConstant | DescriptionDescription | HodnotaValue |
|---|---|---|
CompareMethod.Binary |
Provádí binární porovnání.Performs a binary comparison | 00 |
CompareMethod.Text |
Provádí textové porovnání.Performs a textual comparison | 11 |