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

Description

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

Syntax

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


[ComVisibleAttribute(false)]/*from   w w w  .  j a v a  2  s .c  om*/
public string[] Split(
  string[] separator,
  int count,
  StringSplitOptions options
)

Parameters

String.Split(String[], Int32, 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.
  • count - The maximum number of substrings to return.
  • 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[], Int32, StringSplitOptions) method returns

Example

The following example uses the StringSplitOptions enumeration to include or exclude substrings generated by the Split method.


using System;//from   w  w  w  .j a  va  2s.co m

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

    public static void Show(string[] entries)
    {
        foreach (string entry in entries){
            Console.WriteLine(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