C# String Split(String[], StringSplitOptions)

Description

String Split(String[], StringSplitOptions) returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements.

Syntax

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


[ComVisibleAttribute(false)]/*from   w w w. ja v  a2 s  .  co  m*/
public string[] Split(
  string[] separator,
  StringSplitOptions options
)

Parameters

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

  • separator - An array of single-character strings that delimit the substrings in this string, an empty array that contains no delimiters, or null.
  • options - RemoveEmptyEntries to omit empty array elements from the array returned; or None to include empty array elements in the array returned.

Returns

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

Example

The following example illustrates the difference in the arrays returned by calling a string's String.Split(String[], StringSplitOptions) method with its options parameter equal to StringSplitOptions.None and StringSplitOptions.RemoveEmptyEntries.


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

class Example 
{
   public static void Main() 
   {
      string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
      string[] stringSeparators = new string[] {"[stop]"};
      string[] result;

      result = source.Split(stringSeparators, StringSplitOptions.None);
      Console.WriteLine("Result including all elements ({0} elements):", 
                        result.Length);
   }
}

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