TechAid24

Your Technical Consultant

C# “String.Split()” – Split Strings by Comma, Space, or Multiple Delimiters

Posted on by Anonymous Guest 2 minutes read

The String.Split() method returns an array of strings generated by splitting of original string, separated by the delimiters passed. The delimiters can be a character, 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.

 

String Split using a character

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);

 

String Split using an array of 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.

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);
}

 

String Split using an array of strings

The String Split method can also separate a string based on a substring or several substrings 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);
String.Split()” String Split Method in C#

Anonymous Guest

An anonymous author is a writer who conceals their true identity when publishing content, often for privacy, security, or confidentiality reasons. This approach is often used in technical articles to focus on the information being presented rather than the author's personal identity.

Leave a Reply

Your email address will not be published. Required fields are marked *