C# String Split(Char[])

Description

String Split(Char[]) returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.

Syntax

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


public string[] Split(
  params char[] separator
)

Parameters

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

Returns

String.Split(Char[]) method returns

Example

The following example demonstrates how to extract individual words from a block of text by treating white space and punctuation marks as delimiters.


using System;//from   w ww. ja  v a 2 s  .c o m

public class SplitTest {
    public static void Main() {

        string words = "This is a test\tand a tab character.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

        foreach (string s in split) {
            if (s.Trim() != "")
                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