Search:

Custom Search
_________________________________________________________________

Saturday, May 8, 2010

Split function in C#

Here is a small post about using split function in c#. We are going to split a String by using delimiters. A delimiter will tell when to split the string. So here is a fragment of code to perform splitting.

String answer = "This$is|a|string";

char[] delimitator1 = new char[] { '$' };

char[] delimitator2 = new char[] { '|' };

string[] words = answer.Split(delimitator1);

Console.WriteLine("First word : " + words[0]);

//Note that now we split the second part of the string

String allRest = words[1];

string[] rest1 = allRest.Split(delimitator2);

Console.WriteLine("Second word : " + rest1[0]);

Console.WriteLine("Third word : " + rest1[1]);

Console.WriteLine("Fourth word : " + rest1[2]);


Output:

1 comment:

Babu Kumararasamy said...

char[] delimitator1 = new char[] { '$', '|' };
string[] words = answer.Split(delimitator1);

This is as simple.