C# Split String
The String.Split() method splits a string into an array of strings separated by the split delimiters. 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.
Console.WriteLine("Comma separated strings");
// String of authors
string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";
// Split authors separated by a comma followed by space
string[] authorsList = authors.Split(", ");
foreach (string author in authorsList)
Console.WriteLine(author);
Listing 1.
The result of the 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 strings based on multiple characters in the same method. The Split method takes an argument of an array of characters and splits the 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.
Console.WriteLine("Split with multiple separators");
// Split with multiple separators
string multiCharString = "Mahesh..Chand, Henry\n He\t, Chris-Love, Raj..Beniwal, Praveen-Kumar";
// Split authors separated by a comma followed by space
string[] multiArray = multiCharString.Split(new Char [] {' ', ',', '.', '-', '\n', '\t' } );
foreach (string author in multiArray)
{
if (author.Trim() != "")
Console.WriteLine(author);
}
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 being separated by two substrings.
Console.WriteLine("Split String delimited by another string");
string stringString = "Mahesh Chand, Neel Chand Beniwal, Raj Beniwal, Dinesh Beniwal";
// String separator
string[] stringSeparators = new string[] { "Beniwal, ", "Chand, " };
string[] firstNames = stringString.Split(stringSeparators, StringSplitOptions.None );
foreach (string firstName in firstNames)
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.
using System;
namespace SplitString
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Comma separated strings");
// String of authors
string authors = "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar";
// Split authors separated by a comma followed by space
string[] authorsList = authors.Split(", ");
foreach (string author in authorsList)
Console.WriteLine(author);
Console.WriteLine("Split with multiple separators");
// Split with multiple separators
string multiCharString = "Mahesh..Chand, Henry\n He\t, Chris-Love, Raj..Beniwal, Praveen-Kumar";
// Split authors separated by a comma followed by space
string[] multiArray = multiCharString.Split(new Char [] {' ', ',', '.', '-', '\n', '\t' } );
foreach (string author in multiArray)
{
if (author.Trim() != "")
Console.WriteLine(author);
}
Console.WriteLine("Split String delimited by another string");
string stringString = "Mahesh Chand, Neel Chand Beniwal, Raj Beniwal, Dinesh Beniwal";
// String separator
string[] stringSeparators = new string[] { "Beniwal, ", "Chand, " };
string[] firstNames = stringString.Split(stringSeparators, StringSplitOptions.None );
foreach (string firstName in firstNames)
Console.WriteLine(firstName);
Console.ReadKey();
}
}
}
Listing 4.
The result of the Listing 4 code example looks like Figure 2.
Figure 2. String.Split example
Learn more on C# Strings
Complete Tutorial on C# Strings
Here are some more similar articles related to strings in .NET with C#.
- How to compare strings In C#
- How to use string interpolation In C#
- How to trim strings In C#
- How to use StringBuilder In C#
- How to change a string to uppercase and lowercase in C#
- How to remove and replace strings in C#
- How to apply padding in strings in C#
- How to use a StringCollection in C#
- How to create and use substrings in C#
- How to convert a byte array to a string in C#
- How to create a random string in C#
- How to split a string in C#

Phatboi StudiosPosted Oct 16, 2023, 3:37 PM
Can you suggest an elegant way of doing this but appending 'and' the last item? For instance instead of "Mahesh Chand, Henry He, Chris Love, Raj Beniwal, Praveen Kumar" - it would be "Mahesh Chand, Henry He, Chris Love, Raj Beniwal and Praveen Kumar" - if this is coming from a dynamic list and not a hard-coded string[]
Simmy✌Posted Jul 8, 2020, 11:09 AM
Hi . I have a string input as 5000.0 and I want to make it as 5000 . how to do ? plz help
Farhan AhmedPosted Jan 28, 2020, 4:44 PM
How separated ArrayList numbers with comma?
Rajesh VaishnavPosted Jul 18, 2019, 12:34 PM
String[] authorsList = authors.Split(','); will be used
Vb NjPosted Jul 9, 2019, 1:48 PM
String[] authorsList = authors.Split(", "); Gives "CS1503 Argument 1: cannot convert from 'string' to 'char'".
Vb NjPosted Jul 9, 2019, 1:46 PM
Listing 1 does not work for me. I'm using Visual Studio 2019/.NET 4.7.2.
Kamal MehtaPosted May 23, 2019, 3:51 PM
Great compilation. Would be nice if you highlights some of the problems or caveats it comes with. Especially incorrect usage can cause memory issues in production
Kasun CharithaPosted Apr 10, 2019, 7:29 AM
I want to split character by character . how to do? ex 1994 >>> 1 9 9 4
Rushi MehtaPosted Sep 24, 2018, 4:23 AM
Nic eArticle
Mahesh ChandPosted Sep 22, 2018, 11:15 AM
Article updated and formatted for .NET Core
conrad mbaPosted Feb 18, 2018, 2:58 AM
Nice one sir. well understood. Thanks
حارث منصورPosted Oct 2, 2017, 5:46 PM
Hello anyone can do this in Visual studio?? Take a string of comma separated values/names from the user. Split the string into multiple sub-strings based on the commas.Add all these sub-strings to a list.
Ajeet MishraPosted Jul 19, 2016, 5:50 AM
Nice
Amit Kumar SinghPosted Feb 22, 2016, 3:44 AM
Nice one sir
Upendra Pratap ShahiPosted Jun 29, 2015, 4:49 AM
nice sir..
Girish IyerPosted Jun 9, 2015, 1:20 AM
Nice article
JUKE BOXPosted Dec 12, 2014, 3:21 PM
what is the significance of stringsplitoptions.none here????