C# String Split()

The Split() method breaks up a string at the specified separator and returns its substrings.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string text = "C# is a fun programming language";

// split string string[] result = text.Split(" ");
Console.Write("Result: "); foreach(String str in result) { Console.Write(str + ", "); } Console.ReadLine(); } } } // Output: Result: C#, is, a, fun, programming, language,

Split() Syntax

The syntax of the string Split() method is:

Split(String separator, Int32 count, StringSplitOptions options)

Here, Split() is a method of class String.


Split() Parameters

The Split() method takes the following parameters:

  • separator - separates the substrings in a string
  • count - controls the number of resulting substrings
  • options - specifies whether to omit empty array elements or to include them

Split() Return Value

The Split() method returns a string array containing the substrings.


Example 1: Split String Into an Array

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string text = "a::b::c::d::e";

// split string at :: string[] result = text.Split("::");
foreach(string str in result) { Console.WriteLine(str); } Console.ReadLine(); } } }

Output

a
b
c
d
e

Here, we split the string at ::. Since the count parameter is not passed, the returned array contains all the substrings.


Example 2: Split String Using Multiple Characters

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string text = "a b,c.d/e";

// split string string[] result = text.Split(' ', ',', '.','/');
foreach(string str in result) { Console.WriteLine(str); } Console.ReadLine(); } } }

Output

a
b
c
d
e

Here, we split the string at characters ' ', ',', '.' , and '/'.


Example 3: Split String Delimited by a String or String Array

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string text = "Programiz is developed for programmers";
      string[] separators = {"is", "for"};

// separates string at separators string[] result = text.Split(separators, StringSplitOptions.None);
foreach(string str in result) { Console.WriteLine(str); } Console.ReadLine(); } } }

Output

Programiz
 developed
 programmers

In the above example, notice the line,

string[] result = text.Split(separators, StringSplitOptions.None);

Here,

  • separators - string array used to split the string text at "is" and "for"
  • StringSplitOptions.None - includes empty array elements in the returned array

Example 4: Return a Specific Number of Substrings

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string text = "a::b::c::d::e::f";

      string[] separators = {"::"};
  
// create a maximum of 2 substrings string[] result = text.Split(separators, 2, StringSplitOptions.None);
foreach(string str in result) { Console.WriteLine(str); } Console.ReadLine(); } } }

Output

a
b::c::d::e::f

In the above example, notice the line,

string[] result = text.Split(separators, 2, StringSplitOptions.None);

Here, we have specified the count to 2, so it returns a maximum number of 2 substrings.

Did you find this article helpful?