Export (0) Print
Expand All

String.Split Method (Char[])

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

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 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. The following table lists the white-space characters recognized by the Split method. (It is slightly different than the white-space characters recognized by the String.Trim method.)

Unicode name

Unicode code point

Remarks

CHARACTER TABULATION

U+0009

LINE FEED

U+000A

LINE TABULATION

U+000B

FORM FEED

U+000C

CARRIAGE RETURN

U+000D

SPACE

U+0020

NEXT LINE

U+0085

Introduced in the Microsoft .NET Framework version 2.0.

NO-BREAK SPACE

U+00A0

OGHAM SPACE MARK

U+1680

EN QUAD

U+2000

EM QUAD

U+2001

EN SPACE

U+2002

EM SPACE

U+2003

THREE-PER-EM SPACE

U+2004

FOUR-PER-EM SPACE

U+2005

SIX-PER-EM SPACE

U+2006

FIGURE SPACE

U+2007

PUNCTUATION SPACE

U+2008

THIN SPACE

U+2009

HAIR SPACE

U+200A

ZERO WIDTH SPACE

U+200B

.NET Framework versions 1.0 and 1.1 only.

LINE SEPARATOR

U+2028

PARAGRAPH SEPARATOR

U+2029

IDEOGRAPHIC SPACE

U+3000

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.

For example:

String value

separator

Returned array

"42, 12, 19"

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

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

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

"42..12..19"

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

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

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

"Banana"

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

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

{"Banana"}

"Darb\nSmarba" (C#)

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

new Char[] {} (C#)

Char() = {} (Visual Basic)

{"Darb", "Smarba"}

"Darb\nSmarba" (C#)

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

null (C#)

Nothing (Visual Basic)

{"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 example demonstrates how to extract individual words from a block of text by treating white space and punctuation marks as delimiters. The character array passed to the separator parameter of the String.Split(Char[]) method consists of a space character together with some common punctuation symbols.

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

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft