8 out of 29 rated this helpful - Rate this topic

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
)

Parameters

separator
Type: System.Char[]
An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or null.

Return Value

Type: System.String[]
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 null or contains no characters, white-space characters are assumed to be the delimiters. White-space characters are defined by the Unicode standard and return true if they are passed to the Char.IsWhiteSpace 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. The following table provides examples.

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.

Notes to Callers

In the .NET Framework 3.5 and earlier versions, if the Split method is passed a separator that is null or contains no characters, the method uses a slightly different set of characters to split the string than the Trim method does to trim the string. In the .NET Framework 4, both methods use an identical set of Unicode white-space characters.

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 and a tab 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" +
                       "\tand a tab character.";

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

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}
// The example displays the following output to the console:
//       This
//       is
//       a
//       list
//       of
//       words
//       with
//       a
//       bit
//       of
//       punctuation
//       and
//       a
//       tab
//       character


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Regarding need for Char array...
Ummm, dude.  This is the String.Split (Char[]) implementation.  Your example is for the String[] version.  The Split method is overloaded.  Check out the overload list under Split Method. 
http://msdn.microsoft.com/en-us/library/y7h14879.aspx
Reading formatted text files in C#

How do i read the following line so i can easily use the numbers for other purposes:


1THF O1 1 2.159 -0.085 0.730 -0.2080 0.0166 0.2936

Notice there is a "1" in front of "THF" . How do I split this and convert it to an integer for use in further computation. similarly the floating point values.

A Possible Solution

There are numerous possible solutions. For example, you can use the String.Split method to split the string into substrings on whitespace characters, then use a regular expression to extract the numeric value from the substring. Or you could use String.Split to extract substrings, then call the Char.IsLetter method to build a numeric string by excluding letters. A third solution, and one that I'd adopt here, is to use a regular expression to extract each whitespace-delimited substring. In the regular expression

\G\w*(\-?\d+(\.\d+)?)\w*\s


the first capturing group captures the string representation of the number.

--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation

You don't need to pass a character array!

This documentation is misleading everyone into thinking that you need to pass a character array for the seperator(s) you want to use.

I only discovered by accident (I'm working in JavaScript at the moment) that because the method specifies the params keyword, you can simply specify a comma seperated list of the seperators you want, e. g.

string [] split = words.Split(' ', ',', '.', ':', '\t');

Also

string[] domains = "www.microsoft.com".Split('.');

This is very handy, if you only need to specify the seperator list in a single place and don't need to specify the extra parameters (e.g.
StringSplitOptions.RemoveEmptyEntries).

What C# is actually does here is converting the arguments into an array of the specified type (e.g. char[]) and if you pass in an actual array, it simply passes it in as is (as if the method didn't have the params keyword).

WARNING! If you pass an empty string to the Split method, you'll get back an array with a single empty string (not very useful). To avoid this, you could either test for the empty string, or use the non params methods, e.g.

string[] domains = "".Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // Yields an array of zero length

Is string.split implementation exacly to specification??

Specification say "If the separator parameter is NULL or contains no characters, white-space characters are assumed to be the delimiters"

I interpreted that the implied " else clause" is "the characters AND ONLY the characters in the delimiter array are used as delimeters"

In my application the delimeter array contained only element with 1 character [ ÿ] but split one of the tokens at a space character.

The application was attempting to extract data from a file containing one line per record with fields values delimited - there was no restriction on the field contents apart from not containg the above delimiter.

If Microsoft read this could they consider that in documentation every 'if' should specifically have aan associated 'else' .

The documentation is accurate


I don't understand your point about an implied else clause or an associated else. However, the documentation is accurate.

String.Split uses the individual characters in the separator array to split a string into individual substrings. It does not use any other characters to split the string. However, if the separator array is null or empty, it splits the string on whitespace characters. If separator is non-null, has at least one element, and does not contain any elements that are whitespace characters, the method does not separate substrings on white space.

If you are certain that a whitespace character isn't included in your separator array and you suspect a bug, you can use Microsoft Connect (http://connect.microsoft.com/VisualStudio) to report it, and we will investigate.

--Ron Petrusha
Common Language Runtime Developer Content
Microsoft Corporation