---
title: "String Split in C#"  
description: "In this article I’m explaining about Split () method of string class in C#."  
author: "Anonymous User"  
published: 2014-11-13  
updated: 2020-01-12  
canonical: https://www.mindstick.com/articles/1527/string-split-in-c-sharp  
category: "c#"  
tags: ["string", "split"]  
reading_time: 4 minutes  

---

# String Split in C#

In this article, I’m explaining about a **Split ()** method of **String [class in C#](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example)**.\

## Split:

Here into the [ASP.Net C#](https://www.mindstick.com/forum/435/textchange-event-at-server-side-using-javascript-in-asp-dot-net-c-sharp), Split [string function](https://www.mindstick.com/articles/718/some-important-string-function-in-php) provides the [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) to split the string into a [string array](https://www.mindstick.com/forum/12803/check-for-any-item-in-string-array) by specifying its delimiters.

The C# split string function splits the string into array [collection](https://www.mindstick.com/articles/1718/collections-in-java) according to the number of separators passed to the split function.

Where ASP.Net C# split string function removes the delimiters from the string and stores each part separated at consecutive indexes of an array object.

According to ASP.Net 2.0, C# split string function has 6 overloads as follows.

### Split Function is overloaded

1. The public string [] Split (char [] separator)
2. The public string [] Split (char [] separator, int count)
3. The public string [] Split (char [] separator, StringSplitOptions options)
4. The public string [] Split (string [] separator, StringSplitOptions options)
5. The public string [] Split (char [] separator, int count, StringSplitOptions options)
6. The public string [] Split (string [] separator, int count, StringSplitOptions options)

### Example:

Public string [] Split (char [] separator)

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

The character array passed to the separator parameter of the String. Split (Char[]) method consists of a space character and a tab character, together with some common punctuation symbols.

```
namespace Split{    class Program    {        static void Main(string[] args)        {            string words = "a,b,c,d,e,f,g,h,I,j,k,l,m";             string[] split = words.Split(new char[]{','});             foreach (string word in split)            {                Console.Write(word+"
");            }              Console.ReadLine();          }    }}
```

### Output\
\
![String Split in C#](https://www.mindstick.com/mindstickarticle/996c0a4b-f9c0-4381-85a0-41da6d0c7f63/images/fa6b87e8-8920-4c73-8087-c2b38eb9ed73.png)

**public string [] Split (char [] separator, int count)**

This example demonstrates how to count affects the number of the string returned by Split.

```
namespace split2{    class Program    {        public static void Main()        {            string words = "a,b,c,d,e,f,g,h,i,j";             string[] split = words.Split(new char[] { ',' }, 3);             foreach (string word in split)            {                Console.WriteLine(word);            }            Console.ReadLine();       
}    }}
```

The above example returned three substrings as specified in the count parameter of the C# split string function.

It returned first and the second substring separated from the specified delimiter and third substring unchanged.

##### Output

![String Split in C#](https://www.mindstick.com/mindstickarticle/996c0a4b-f9c0-4381-85a0-41da6d0c7f63/images/df866e45-2d9b-423f-972d-815ea054c762.png)

**Public string [] Split (char [] separator, StringSplitOptions options)**

The following example uses the StringSplitOptions [enumeration](https://www.mindstick.com/blog/88/enumeration-in-c-sharp) to include or exclude substrings generated by the Split () method.

```
namespace Split3{    class Program    {        public static void Main()        {            string s1 = ",ONE,,TWO,,,THREE,,";             char[] delimiter = new char[] { ',' };             string[] stringSeparators = new string[] { "[stop]" };             string[] result;             Console.WriteLine("1)The original string is. {0}", s1);             Console.WriteLine("The delimiter character is. '{0}'", delimiter[0]);             result = s1.Split(delimiter, StringSplitOptions.None);             Show(result);             Console.WriteLine("1) Split a string delimited by characters and " +"return all non-empty elements:");             result = s1.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);             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);            }            Console.ReadLine();        }    }} 
```

##### Output

![String Split in C#](https://www.mindstick.com/mindstickarticle/996c0a4b-f9c0-4381-85a0-41da6d0c7f63/images/c704ea19-5f5a-4c68-8b08-0bdc928706ba.png)

**Public string [] Split (string [] separator, StringSplitOptions options)**

The following example defines an array of separator that includes punctuation and white-space [characters](https://answers.mindstick.com/qa/42112/who-is-the-originator-of-avengers-characters).

Passing this array along with a value of StringSplitOptions.RemoveEmptyEntries to the Split (String [], StringSplitOptions) [method returns](https://www.mindstick.com/forum/12904/main-method-returns-if-submethod-has-stoped-by-return-in-c-sharp) an array that consists of the individual words from the string.

```
namespace Split4{    class Program    {        static void Main(string[] args)        {            string[] separators = { ",", ".", "!", "?", ";", ":", " " };             string value = "MindStick,Software!pvt.?ltd..:";             string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);             foreach (var word in words)                Console.Write(word+"
");             Console.ReadLine();        }    }} 
```

##### Output

![String Split in C#](https://www.mindstick.com/mindstickarticle/996c0a4b-f9c0-4381-85a0-41da6d0c7f63/images/5c607184-94c8-42f9-8243-ed13632737bf.png)

## public string [] Split (char [] separator, int count, StringSplitOptions options)

```
namespace Split5{    class Program    {        static void Main(string[] args)        {            string s1 = ",ONE,,TWO,,,THREE,,";             char[] delimiter = new char[] { ',' };             string[] stringSeparators = new string[] { "[stop]" };             string[] result;             Console.WriteLine("1)The
original string is. {0}", s1);             Console.WriteLine("The
delimiter character is. '{0}'", delimiter[0]);             result = s1.Split(delimiter,3, StringSplitOptions.None);             Show(result);             Console.WriteLine("1) Split a
string delimited by characters and " + "return all non-empty elements:");             result = s1.Split(delimiter,2, StringSplitOptions.RemoveEmptyEntries);            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);            }            Console.ReadLine();        }    }}
```

Output

![String Split in C#](https://www.mindstick.com/mindstickarticle/996c0a4b-f9c0-4381-85a0-41da6d0c7f63/images/108305ad-f109-4491-8b10-732cb37b7f4d.png)

Into my next post, I'll discuss about **simple tooltip using HTML CSS**

---

Original Source: https://www.mindstick.com/articles/1527/string-split-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
