Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Split Method (Char[], Int32)

Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char array. A parameter specifies the maximum number of substrings to return.

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

public string[] Split (
	char[] separator,
	int count
)
public String[] Split (
	char[] separator, 
	int count
)
public function Split (
	separator : char[], 
	count : int
) : String[]

Parameters

separator

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

count

The maximum number of substrings to return.

Return Value

An array whose elements contain the substrings in this instance that are delimited by one or more characters in separator. For more information, see the Remarks section.

Exception typeCondition

ArgumentOutOfRangeException

count is negative.

Delimiter characters 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 characters 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 count is zero, an empty array is returned.

If 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.

For example:

Input

separator, count

Output

"42, 12, 19"

new Char[] {',', ' '}, 2

{"42", " 12, 19"}

"42..12..19"

new Char[] {'.'}, 4

{"42", "", "12", ".19"}

"Banana"

new Char[] {'.'}, 2

{"Banana"}

"Darb\nSmarba"

new Char[] {}, 1

{"Darb\nSmarba"}

"Darb\nSmarba"

new Char[] null, 2

{"Darb", "Smarba"}

"Darb\nSmarba"

new Char[] null, 100

{"Darb", "Smarba"}

The following code example demonstrates how count affects the number of strings returned by Split.

using System;

public class StringSplit2 {
    public static void Main() {

        string delimStr = " ,.:";
	char [] delimiter = delimStr.ToCharArray();
        string words = "one two,three:four.";
        string [] split = null;

	Console.WriteLine("The delimiters are -{0}-", delimStr);
	for (int x = 1; x <= 5; x++) {
	    split = words.Split(delimiter, x);
            Console.WriteLine("\ncount = {0,2} ..............", x);
	    foreach (string s in split) {
                Console.WriteLine("-{0}-", s);
      	    }
	}
    }
}

import System.*;

public class StringSplit2
{
    public static void main(String[] args)
    {
        String delimStr = " ,.:";
        char delimiter[] = delimStr.ToCharArray();
        String words = "one two,three:four.";
        String split[] = null;

        Console.WriteLine("The delimiters are -{0}-", delimStr);
        for (int x = 1; x <= 5; x++) {
            split = words.Split(delimiter, x);
            Console.WriteLine("\ncount = {0,2} ..............",
                System.Convert.ToString(x));
            for (int iCtr = 0; iCtr < split.get_Length(); iCtr++) {
                String s = split[iCtr];
                Console.WriteLine("-{0}-", s);
            }
        }
    } //main
} //StringSplit2

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, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

Show:
© 2015 Microsoft