Split Value with System.Text.RegularExpressions.Regex.Split in C#

There are many ways to split the value in C# but the shortest yet efficient way is to use Split method of RegularExpression.Regex class. Let us see how it works. I will show you this example in Windows Application using C# but you can use same concept in ASP.NET with C#. If you want to use it in VB.NET than minor syntax change should be made in the function but the concept, class and method will remain same.

Let us one new Windows Application with C# and open code windows of FORM after creating one Label in windows FORM.

private void Form1_Load(object sender, EventArgs e)

{

label1.Text = “”;

string strTest = “1!800!200,19!1,0!2!!114”;

string[] strSplitedValue = returnSplittedArray(strTest, “!”);

foreach (string str in strSplitedValue)

{

if (str.Length > 0)

{

label1.Text += str;

label1.Text += “\n”;

}

}

}

public static string[] returnSplittedArray(string fullString,string separator)

{

string[] splitArray;

splitArray = System.Text.RegularExpressions.Regex.Split(fullString, System.Text.RegularExpressions.Regex.Escape(separator));

return splitArray;

}

That’s it, you are done, returnSplittedArray is very small and handy function for anyone.

Happy Programming!!

Reference: Ritesh Shah

http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com