Export (0) Print
Expand All

String.Split Method (Char[], Int32)

.NET Framework 1.1

Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array. A parameter specifies the maximum number of array elements to return.

[Visual Basic]
Overloads Public Function Split( _
   ByVal separator() As Char, _
   ByVal count As Integer _
) As String()
[C#]
public string[] Split(
 char[] separator,
 int count
);
[C++]
public: String* Split(
 __wchar_t separator __gc[],
 int count
)  __gc[];
[JScript]
public function Split(
   separator : Char[],
 count : int
) : 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).
count
The maximum number of array elements to return.

Return Value

An array consisting of a single element containing this instance, if this instance contains none of the characters in separator.

-or-

An array of substrings if this instance is delimited by one or more of the characters in separator.

-or-

An array of the substrings in this instance delimited by white space characters if those characters occur and separator is a null reference (Nothing in Visual Basic) or contains no delimiter characters.

Empty is returned for any substring where two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance.

Delimiter characters are not included in the substrings.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException count is negative.

Remarks

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 substrings are returned and no exception is thrown.

For example:

Input separator, count Output
"42, 12, 19" new Char[] {',', ' '}, 2 {"42", " 12, 19"}
"42..12..19" new Char[] {'.'}, 4 {"42", "", "12", ".19"}
"Banana" new Char[] {'.'}, 2 {"Banana"}
"Darb\nSmarba" new Char[] {}, 1 {"Darb\nSmarba"}
"Darb\nSmarba" new Char[] null, 2 {"Darb", "Smarba"}
"Darb\nSmarba" new Char[] null, 100 {"Darb", "Smarba"}

Example

[Visual Basic, C#, C++] The following code example demonstrates how count affects the number of strings returned by Split.

[Visual Basic] 
Imports System
Imports Microsoft.VisualBasic
 _

Public Class StringSplit2
   
   Public Shared Sub Main()
      
      Dim delimStr As String = " ,.:"
      Dim delimiter As Char() = delimStr.ToCharArray()
      Dim words As String = "one two,three:four."
      Dim split As String() = Nothing
      
      Console.WriteLine("The delimiters are -{0}-", delimStr)
      Dim x As Integer
      For x = 1 To 5
         split = words.Split(delimiter, x)
         Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x)
         Dim s As String
         For Each s In  split
            Console.WriteLine("-{0}-", s)
         Next s
      Next x
   End Sub 'Main
End Class 'StringSplit2

[C#] 
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);
              }
    }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

int main()
{

   String* delimStr = S" ,.:";
   Char delimiter[] = delimStr->ToCharArray();
   String* words = S"one two,three:four.";
   String* split[] = 0;

   Console::WriteLine(S"The delimiters are -{0}-", delimStr);
   for (int x = 1; x <= 5; x++) {
      split = words->Split(delimiter, x);
      Console::WriteLine(S"\ncount = {0, 2} ..............", __box(x));
      IEnumerator* myEnum = split->GetEnumerator();
      while (myEnum->MoveNext()) {
         String* s = __try_cast<String*>(myEnum->Current);
         Console::WriteLine(S"-{0}-", s);
      }
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.Split Overload List | Char | Array | Int32 | Concat | Insert | Join | Remove | Replace | Substring | Trim

Show:
© 2014 Microsoft