C# parse strings using Split method

by Jeff Fillegar on August 1, 2009

class TestStringSplit
{
static void Main()
{
char[] delimiterChars = { ‘ ‘, ‘,’, ‘.’, ‘:’, ‘\t’ };

string text = “one\ttwo three:four,five six seven”;
System.Console.WriteLine(“Original text: ‘{0}'”, text);

string[] words = text.Split(delimiterChars);
System.Console.WriteLine(“{0} words in text:”, words.Length);

foreach (string s in words)
{
       System.Console.WriteLine(s);
}
}
}

Output

 
Original text: 'one     two three:four,five six seven'
7 words in text:
one
two
three
four
five
six
seven

Leave a Comment

Previous post:

Next post: