Export (0) Print
Expand All

String.Split Method (String[], Int32, StringSplitOptions)

Note: This method is new in the .NET Framework version 2.0.

Returns a String array containing 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.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(false)] 
public string[] Split (
	string[] separator,
	int count,
	StringSplitOptions options
)
/** @attribute ComVisibleAttribute(false) */ 
public String[] Split (
	String[] separator, 
	int count, 
	StringSplitOptions options
)
ComVisibleAttribute(false) 
public function Split (
	separator : String[], 
	count : int, 
	options : StringSplitOptions
) : String[]

Parameters

separator

An array of strings that delimit the substrings in this string, an empty array containing no delimiters, or a null reference (Nothing in Visual Basic).

count

The maximum number of substrings to return.

options

Specify RemoveEmptyEntries to omit empty array elements from the array returned, or None to include empty array elements in the array returned.

Return Value

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.

Exception typeCondition

ArgumentOutOfRangeException

count is negative.

ArgumentException

options is not one of the StringSplitOptions values.

Return Value Details

Delimiter strings are not included in the elements of the return value array. Delimiters are detected using an ordinal comparison.

If this instance does not contain any of the strings in separator, the return value array consists of a single element that contains this instance. If the separator parameter is a null reference (Nothing in Visual Basic) or contains no characters, white space characters are assumed to be the delimiters.

If the count parameter is zero, or the options parameter is RemoveEmptyEntries and the length of this instance is zero, an empty array is returned.

If the options parameter is None, and two delimiters are adjacent or a delimiter is found at the beginning or end of this instance, the corresponding array element contains 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.

Comparison Details

The Split 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 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 Split method ignores any element of separator whose value is a null reference (Nothing in Visual Basic) or the empty string ("").

To avoid ambiguous results when strings in separator have characters in common, the 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 following code example uses the StringSplitOptions enumeration to include or exclude substrings generated by the Split method.

// This example demonstrates the String() methods that use
// the StringSplitOptions enumeration.
using System;

class Sample 
{
    public static void Main() 
    {
    string s1 = ",ONE,,TWO,,,THREE,,";
    string s2 = "[stop]" +
                "ONE[stop][stop]" +
                "TWO[stop][stop][stop]" +
                "THREE[stop][stop]";
    char[] charSeparators = new char[] {','};
    string[] stringSeparators = new string[] {"[stop]"};
    string[] result;
// ------------------------------------------------------------------------------
// Split a string delimited by characters.
// ------------------------------------------------------------------------------
    Console.WriteLine("1) Split a string delimited by characters:\n");

// Display the original string and delimiter characters.
    Console.WriteLine("1a )The original string is \"{0}\".", s1);
    Console.WriteLine("The delimiter character is '{0}'.\n", 
                       charSeparators[0]);

// Split a string delimited by characters and return all elements.
    Console.WriteLine("1b) Split a string delimited by characters and " +
                      "return all elements:");
    result = s1.Split(charSeparators, StringSplitOptions.None);
    Show(result);

// Split a string delimited by characters and return all non-empty elements.
    Console.WriteLine("1c) Split a string delimited by characters and " +
                      "return all non-empty elements:");
    result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
    Show(result);

// Split the original string into the string and empty string before the 
// delimiter and the remainder of the original string after the delimiter.
    Console.WriteLine("1d) Split a string delimited by characters and " +
                      "return 2 elements:");
    result = s1.Split(charSeparators, 2, StringSplitOptions.None);
    Show(result);

// Split the original string into the string after the delimiter and the 
// remainder of the original string after the delimiter.
    Console.WriteLine("1e) Split a string delimited by characters and " +
                      "return 2 non-empty elements:");
    result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
    Show(result);

// ------------------------------------------------------------------------------
// Split a string delimited by another string.
// ------------------------------------------------------------------------------
    Console.WriteLine("2) Split a string delimited by another string:\n");

// Display the original string and delimiter string.
    Console.WriteLine("2a) The original string is \"{0}\".", s2);
    Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]);

// Split a string delimited by another string and return all elements.
    Console.WriteLine("2b) Split a string delimited by another string and " +
                      "return all elements:");
    result = s2.Split(stringSeparators, StringSplitOptions.None);
    Show(result);

// Split the original string at the delimiter and return all non-empty elements.
    Console.WriteLine("2c) Split a string delimited by another string and " +
                      "return all non-empty elements:");
    result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
    Show(result);

// Split the original string into the empty string before the 
// delimiter and the remainder of the original string after the delimiter.
    Console.WriteLine("2d) Split a string delimited by another string and " +
                      "return 2 elements:");
    result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
    Show(result);

// Split the original string into the string after the delimiter and the 
// remainder of the original string after the delimiter.
    Console.WriteLine("2e) Split a string delimited by another string and " + 
                      "return 2 non-empty elements:");
    result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
    Show(result);
    }

// Display the array of separated strings.
    public static void Show(string[] entries)
    {
    Console.WriteLine("The return value contains these {0} elements:", entries.Length);
    foreach (string entry in entries)
        {
        Console.Write("<{0}>", entry);
        }
    Console.Write("\n\n");
    }
}
/*
This example produces the following results:

1) Split a string delimited by characters:

1a )The original string is ",ONE,,TWO,,,THREE,,".
The delimiter character is ','.

1b) Split a string delimited by characters and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

1c) Split a string delimited by characters and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>

1d) Split a string delimited by characters and return 2 elements:
The return value contains these 2 elements:
<><ONE,,TWO,,,THREE,,>

1e) Split a string delimited by characters and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO,,,THREE,,>

2) Split a string delimited by another string:

2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
The delimiter string is "[stop]".

2b) Split a string delimited by another string and return all elements:
The return value contains these 9 elements:
<><ONE><><TWO><><><THREE><><>

2c) Split a string delimited by another string and return all non-empty elements:
The return value contains these 3 elements:
<ONE><TWO><THREE>

2d) Split a string delimited by another string and return 2 elements:
The return value contains these 2 elements:
<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>

2e) Split a string delimited by another string and return 2 non-empty elements:
The return value contains these 2 elements:
<ONE><TWO[stop][stop][stop]THREE[stop][stop]>

*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft