Split String in C#

In this article we will discuss about how to split the strings in C#.
  • 3678

The Split method separates strings by a specified set of characters and places these strings into an array of strings. The Split method takes an array of separator strings. For example, if you want to separate a string with a space, you would pass space as a separator. The separated strings are returned in an array of strings.

The following code snippet splits sentence string based on spaces and stores all separated strings in an array.

 

string sentence = "Mahesh Chand is an author and founder of C# Corner";
Console
.WriteLine("Original String: {0}", sentence);
string[] stringSeparators = new string[] { " " };
string[] result = sentence.Split(stringSeparators, StringSplitOptions.None);
Console
.WriteLine("Final Strings:");
foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);



Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.