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.
2 out of 2 rated this helpful - Rate this topic

String.Split Method (Char[], Int32)

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character 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[]
Not applicable.

Parameters

separator

An array of Unicode characters that delimit the substrings in this instance, an empty array that contains 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 returned array.

If this instance does not contain any of the characters in separator, the returned 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 the separator parameter is a null reference (Nothing in Visual Basic) or contains no characters, white-space characters are assumed to be the delimiters. For a list of the characters that the Split method interprets as white space, see the table in the Remarks section of the String.Split(Char[]) method. (Note that this list is slightly different from the list of white-space characters recognized by the Trim method.)

Each element of separator defines a separate delimiter character. 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:

String value

separator

count

Returned array

"42, 12, 19"

new Char[] {',', ' '} (C#)

Char() = {","c, " "c} (Visual Basic)

2

{"42", " 12, 19"}

"42..12..19"

new Char[] {'.'} (C#)

Char() = {"."c} (Visual Basic)

4

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

"Banana"

new Char[] {'.'} (C#)

Char() = {"."c} (Visual Basic)

2

{"Banana"}

"Darb\nSmarba" (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

new Char[] {} (C#)

Char() = {} (Visual Basic)

1

{"Darb\nSmarba"} (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

"Darb\nSmarba" (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

new Char[] null (C#)

Char() = Nothing

2

{"Darb", "Smarba"}

"Darb\nSmarba" (C#)

"Darb" & vbLf & "Smarba" (Visual Basic)

new Char[] null (C#)

Char() = Nothing

100

{"Darb", "Smarba"}

Performance Considerations

The 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 IndexOf or IndexOfAny method, and optionally the Compare method, to locate a substring within a string.

If you are splitting a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. If you are splitting a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Then use the 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 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.

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 Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.