C# String Split(Char[], Int32, StringSplitOptions)

Description

String Split(Char[], Int32, StringSplitOptions) returns a string array that contains the substrings in this string that are delimited by elements of a specified Unicode character array. Parameters specify the maximum number of substrings to return and whether to return empty array elements.

Syntax

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


[ComVisibleAttribute(false)]/*  w w w  .ja  va  2 s  .c o  m*/
public string[] Split(
  char[] separator,
  int count,
  StringSplitOptions options
)

Parameters

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

  • separator - An array of Unicode characters that delimit the substrings in this string, an empty array that contains no delimiters, or null.
  • count - The maximum number of substrings to return.
  • options - StringSplitOptions.RemoveEmptyEntries to omit empty array elements from the array returned; or StringSplitOptions.None to include empty array elements in the array returned.

Returns

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

Example

This example demonstrates the String() methods that use the StringSplitOptions enumeration.


using System;/*w  ww .  j  a v  a  2  s .  com*/

class Sample 
{
    public static void Main() 
    {
        string s1 = ",ONE,,TWO,,,THREE,,";
        char[] charSeparators = new char[] {','};
        string[] result;
    
        result = s1.Split(charSeparators, 2, StringSplitOptions.None);
        Show(result);

    }

    public static void Show(string[] entries)
    {
        Console.WriteLine("The return value contains these {0} elements:", entries.Length);
        foreach (string entry in entries){
           Console.Write("<{0}>", entry);
        }
    }
}

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