C# String Split(Char[], Int32)

Description

String Split(Char[], Int32) 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.

Syntax

String.Split(Char[], Int32) has the following syntax.


public string[] Split(
  char[] separator,
  int count
)

Parameters

String.Split(Char[], Int32) has the following parameters.

  • separator - An array of Unicode characters that delimit the substrings in this instance, an empty array that contains no delimiters, or null.
  • count - The maximum number of substrings to return.

Returns

String.Split(Char[], Int32) method returns

Example

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


// w ww .ja  va 2s  . c  o  m
using System;

public class StringSplit2 
{
   public static void Main() 
   {
      string delimStr = " ,.:";
    char [] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string [] split = null;

    for (int x = 1; x <= 5; x++) 
      {
        split = words.Split(delimiter, x);
          Console.WriteLine(x);
        foreach (string s in split) 
          {
             Console.WriteLine(s);
          }
    }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version