Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.
- separator
- An array of single-character strings that delimit the substrings in this string, an empty array that contains no delimiters, or null.
- count
- The maximum number of substrings to return.
- options
- StringSplitOptions.RemoveEmptyEntries to omit empty array elements from the array returned; or StringSplitOptions.None to include empty array elements in the array returned.
An array whose elements contain the substrings in this string that are delimited by one or more strings in separator. For more information, see the Remarks section.
Delimiter strings are not included in the elements of the returned array.
If this instance does not contain any of the strings in separator, or the count parameter is 1, the returned array consists of a single element that contains this instance. If the separator parameter is null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the char.IsWhiteSpace(char) method. However, if the separator parameter in the call to this method overload is null, compiler overload resolution fails. To unambiguously identify the called method, your code must indicate the type of the null. The following example shows several ways to unambiguously identify this overload.
code reference: System.String.Split#4
If the count parameter is zero, or the options parameter is StringSplitOptions.RemoveEmptyEntries and the length of this instance is zero, an empty array is returned.
Each element of separator defines a separate delimiter that consists of one or more characters. If the options parameter is StringSplitOptions.None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains string.Empty.
If there are more than count substrings in this instance, the first count minus 1 substrings are returned in the first count minus 1 elements of the return value, and the remaining characters in this instance are returned in the last element of the return value.
If count is greater than the number of substrings, the available substrings are returned and no exception is thrown.
If any of the elements in separator consists of more than a single character, only the first character of that element is considered a delimiter, and any subsequent characters are ignored. For example, if one of the elements in separator is "10", attempting to split the string "This10is10a10string." returns the following four-element array: { "This", "0is", "0a", "0string." }.
The string.Split(String[], int, StringSplitOptions) method extracts the substrings in this string that are delimited by one or more of the strings in the separator parameter, and returns those substrings as elements of an array.
The erload:System.String.Split method looks for delimiters by performing comparisons using case-sensitive ordinal sort rules. For more information about word, string, and ordinal sorts, see the System.Globalization.CompareOptions enumeration.
The string.Split(String[], int, StringSplitOptions) method ignores any element of separator whose value is null or the empty string ("").
To avoid ambiguous results when strings in separator have characters in common, the erload:System.String.Split method proceeds from the beginning to the end of the value of the instance, and matches the first element in separator that is equal to a delimiter in the instance. The order in which substrings are encountered in the instance takes precedence over the order of elements in separator.
For example, consider an instance whose value is "abcdef". If the first element in separator was "ef" and the second element was "bcde", the result of the split operation would be "a" and "f". This is because the substring in the instance, "bcde", is encountered and matches an element in separator before the substring "f" is encountered.
However, if the first element of separator was "bcd" and the second element was "bc", the result of the split operation would be "a" and "ef". This is because "bcd" is the first delimiter in separator that matches a delimiter in the instance. If the order of the separators was reversed so the first element was "bc" and the second element was "bcd", the result would be "a" and "def".
The erload:System.String.Split methods allocate memory for the returned array object and a string object for each array element. If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the erload:System.String.IndexOf or erload:System.String.IndexOfAny method, and optionally the erload:System.String.Compare method, to locate a substring within a string.
If you are splitting a string at a separator character, use the erload:System.String.IndexOf or erload:System.String.IndexOfAny method to locate a separator character in the string. If you are splitting a string at a separator string, use the erload:System.String.IndexOf or erload:System.String.IndexOfAny method to locate the first character of the separator string. Then use the erload:System.String.Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
In addition, if the same set of characters is used to split strings in multiple erload:System.String.Split method calls, consider creating a single array and referencing it in each method call. This significantly reduces the additional overhead of each method call.