Sunday, December 9, 2012

How to Split String in C#


In C#.NET we have two methods that we can use for string split.

string.Split()
In this method we need to pass the delimiter as a parameter to the function.

string a = "chamara-janaka";
string[] arr = a.Split('-');
foreach (string item in arr)
{
         Response.Write(item);
         Response.Write("<br/>");
}

RegEx.Split()

Need to use System.Text.RegularExpressions namespace.
string to be split and the delimiter need to be passed as parameters.  

string a = "chamara-janaka";
string[] arr = Regex.Split(a, "-");
foreach (string item in arr)
{
         Response.Write(item);
         Response.Write("<br/>");
}

Out put:
chamara
janaka

No comments:
Write comments
Recommended Posts × +