Split String In C#

Learn different ways to Split Strings in C#. A string can be splitted in C# separated by a character delimiter, string delimiter, and an array of characters.

C# Split String

 
The String.Split() method splits a string into an array of strings separated by the split delimeters. The split delimiters can be a character or an array of characters or an array of strings. The code examples in this article discuss various forms of String.Split method and how to split strings using different delimiters in C# and .NET.
 
Table 1 lists String.Split() method overloaded forms. 
 
Method Description
Split(String[], Int32, StringSplitOptions) Splits a string into a maximum number of substrings based on the strings in an array. You can specify whether the substrings include empty array elements.
Split(Char[], Int32, StringSplitOptions) Splits a string into a maximum number of substrings based on the characters in an array.
Split(String[], StringSplitOptions) Splits a string into substrings based on the strings in an array. You can specify whether the substrings include empty array elements.
Split(Char[]) Splits a string into substrings that are based on the characters in an array.
Split(Char[], StringSplitOptions) Splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements.
Split(Char[], Int32) Splits a string into a maximum number of substrings based on the characters in an array. You also specify the maximum number of substrings to return.
 
Table 1.
 

Split String Into an Array

 
The simplest form of string split is splitting a string into an array of substrings separated by a character such as a comma. Listing 1 is the code example that has a string of author names separated by a comma and a space. The authors.Split method splits the string into an array of author names that are separated by a comma and space. 
  1. Console.WriteLine("Comma separated strings");  
  2. // String of authors  
  3. string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";  
  4. // Split authors separated by a comma followed by space  
  5. string[] authorsList = authors.Split(", ");  
  6. foreach (string author in authorsList)  
  7. Console.WriteLine(author);  
Listing 1.
 
The result of Listing 1 code example looks like Figure 1.
 
 
 
Figure 1. Spilt String in an array of substrings
 

Split String using multiple characters 

 
String.Split method can also separate string based on multiple characters in the same method. The Split method takes an argument of an array of characters and splits string based on all the characters in the array.
 
Listing 2 is an example of splitting a string into an array of strings based on several characters.
  1. Console.WriteLine("Split with multiple separators");  
  2. // Split with multiple separators  
  3. string multiCharString = "Mahesh..Chand, Henry\n He\t, Chris-Love, Raj..Beniwal, Praveen-Kumar";  
  4. // Split authors separated by a comma followed by space  
  5. string[] multiArray = multiCharString.Split(new Char [] {' '',''.''-''\n''\t' } );  
  6. foreach (string author in multiArray)  
  7. {  
  8.     if (author.Trim() != "")  
  9.         Console.WriteLine(author);  
  10. }  
Listing 2. Split String based on an array of characters
 

Split String delimited by a string or an array of strings

 
String.Split method can also separate a string based on a substring or several strings in the string. The Split method takes an argument of an array of substrings or strings.
 
Listing 3 is an example of splitting a string into an array of strings based on separated by two substrings.
  1. Console.WriteLine("Split String delimited by another string");  
  2.               
  3. string stringString = "Mahesh Chand, Neel Chand Beniwal, Raj Beniwal, Dinesh Beniwal";  
  4. // String separator  
  5. string[] stringSeparators = new string[] { "Beniwal, ""Chand, " };  
  6. string[] firstNames = stringString.Split(stringSeparators, StringSplitOptions.None );  
  7. foreach (string firstName in firstNames)  
  8.         Console.WriteLine(firstName);  
Listing 3. Split String based on an array of strings
 
Code Example
 
Listing 4 is the complete code example of Split.String. 
  1. using System;  
  2.   
  3. namespace SplitString  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Comma separated strings");  
  10.             // String of authors  
  11.             string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";  
  12.             // Split authors separated by a comma followed by space  
  13.             string[] authorsList = authors.Split(", ");  
  14.             foreach (string author in authorsList)  
  15.                 Console.WriteLine(author);  
  16.   
  17.             Console.WriteLine("Split with multiple separators");  
  18.             // Split with multiple separators  
  19.             string multiCharString = "Mahesh..Chand, Henry\n He\t, Chris-Love, Raj..Beniwal, Praveen-Kumar";  
  20.             // Split authors separated by a comma followed by space  
  21.             string[] multiArray = multiCharString.Split(new Char [] {' '',''.''-''\n''\t' } );  
  22.             foreach (string author in multiArray)  
  23.             {  
  24.                 if (author.Trim() != "")  
  25.                     Console.WriteLine(author);  
  26.             }  
  27.   
  28.   
  29.             Console.WriteLine("Split String delimited by another string");  
  30.               
  31.             string stringString = "Mahesh Chand, Neel Chand Beniwal, Raj Beniwal, Dinesh Beniwal";  
  32.             // String separator  
  33.             string[] stringSeparators = new string[] { "Beniwal, ""Chand, " };  
  34.             string[] firstNames = stringString.Split(stringSeparators, StringSplitOptions.None );  
  35.             foreach (string firstName in firstNames)  
  36.                     Console.WriteLine(firstName);  
  37.            
  38.             Console.ReadKey();  
  39.         }  
  40.     }  
  41. }  
Listing 4.
 
The result of Listing 4 code example looks like Figure 2.
 
  
Figure 2. String.Split example
 
Learn more on C# Strings