How to split Strings in C#

 
The following code example shows how a string can be parsed using the String.Split method.

String.Split takes an array of chars that indicate which characters are to be used as delimiters.

string str = "Monday;Tuesday;Wednesday;Thursday;Friday;Saturday;Sunday";
char[] delimiterChars = { ';' };
string[] Days = str.Split(delimiterChars);

in above example semicolon is used as delimiter, and Days array returns all days separately you can see Days array as:
Days[0]= “Monday”
Days[0]= “Tuesday”
Days[0]= “Wednesday”
Days[0]= “Thursday”
Days[0]= “Friday”
Days[0]= “Saturday”
Days[0]= Sunday”

Now see this example code:

string str = "Monday;Tuesday,Wednesday Thurseday;Friday.Satureday&Sunday";
char[] delimiterChars = { ' ', ',', '.', ':','&'};
string[] Days = str.Split(delimiterChars);

In this example, spaces, commas, periods, colons, and ‘&’ are used as delimiters. and Days[] contains names of all days separately.
 

One thought on “How to split Strings in C#”

Comments are closed.