Export (0) Print
Expand All

String.Split Method (Char[])

Returns a String array containing the substrings in this instance that are delimited by elements of a specified Char array.

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

public string[] Split (
	params char[] separator
)
public String[] Split (
	char[] separator
)
public function Split (
	... separator : char[]
) : 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).

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.

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

For example:

Input

separator

Output

"42, 12, 19"

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

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

"42..12..19"

new Char[] {'.'}

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

"Banana"

new Char[] {'.'}

{"Banana"}

"Darb\nSmarba"

new Char[] {}

{"Darb", "Smarba"}

"Darb\nSmarba"

null

{"Darb", "Smarba"}

Performance Considerations

The Split methods allocate memory for the return value array object and a String object for each array element. If managing memory allocation is critical in your application, consider using the 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 IndexOfAny method to locate a separator character in the string. If you are splitting a string at a separator string, use the IndexOfAny method to locate the first character of a 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.

The following code example demonstrates how you can tokenize a string with the Split method.

using System;

public class SplitTest {
    public static void Main() {

        string words = "this is a list of words, with: a bit of punctuation.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':'});

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}

import System.*;

public class SplitTest
{
    public static void main(String[] args)
    {
        String words = "this is a list of words, with: a bit of punctuation.";
        String split[] = words.Split(new char[] { ' ', ',', '.', ':' });

        for (int iCtr = 0; iCtr < split.get_Length(); iCtr++) {
            String s = (String)split.get_Item(iCtr);
            if (!(s.Trim().Equals(""))) {
                Console.WriteLine(s);
            }
        }
    } //main
} //SplitTest

import System;

public class SplitTest {
    public static function Main() : void  {

        var words : String = "this is a list of words, with: a bit of punctuation.";
        var separators : char[] = [' ', ',', '.', ':'];
        var split : String [] = words.Split(separators);

        for (var i : int in split) {
            var s : String = split[i];
            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}
SplitTest.Main();

Windows 98, Windows 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 .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, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft