String.Split Method (array<Char>[]()[], Int32)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

Syntax

Public Function Split ( _
    separator As Char(), _
    count As Integer _
) As String()
public string[] Split(
    char[] separator,
    int count
)

Parameters

  • separator
    Type: array<System..::.Char>[]()[]
    An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or nullNothingnullptra null reference (Nothing in Visual Basic).

Return Value

Type: array<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.

Exceptions

Exception Condition
ArgumentOutOfRangeException

count is negative.

Remarks

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

If the separator parameter is nullNothingnullptra null reference (Nothing in Visual Basic) 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.

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.

The following table provides examples.

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.

Notes to Callers

In the .NET Framework 3.5 and earlier versions, if the Split method is passed a separator that is nullNothingnullptra null reference (Nothing in Visual Basic) 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.

Examples

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

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim delimStr As String = " ,.:"
      Dim delimiter() As Char = delimStr.ToCharArray()
      Dim words As String = "one two,three:four."
      Dim split() As String = Nothing

      outputBlock.Text &= "The delimiters are:" & vbCrLf
      For Each ch As Char In delimStr
         outputBlock.Text &= String.Format("   '{0}'", ch) + vbCrLf
      Next
      outputBlock.Text &= vbCrLf

      split = words.Split(delimiter)
      For Each s As String In split
         outputBlock.Text += String.Format("'{0}'", s) & vbCrLf
      Next 
   End Sub 
End Class 
' The example displays the following output:
'       
'       The delimiters are:
'          ''
'          ','
'          '.'
'          ':'
'          
'       'one'
'       'two'
'       'three'
'       'four'
'       ''    
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string delimStr = " ,.:";
      char[] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string[] split = null;

      outputBlock.Text += "The delimiters are:" + "\n";
      foreach (char ch in delimStr)
         outputBlock.Text += String.Format("   '{0}'", ch) + "\n";

      outputBlock.Text += "\n";

      split = words.Split(delimiter);
      foreach (string s in split)
      {
         outputBlock.Text += String.Format("'{0}'", s) + "\n";
      }
   }
}
// The example displays the following output:
//       
//       The delimiters are:
//          ''
//          ','
//          '.'
//          ':'
//          
//       'one'
//       'two'
//       'three'
//       'four'
//       ''    

Version Information

Windows Phone OS

Supported in: 8.1, 8.0

See Also

Reference

String Class

Split Overload

System Namespace

Char

Array

Int32

Concat

Insert

Join

Remove

Replace

Substring

Trim