Splitting string in C#

Posted by in ASP.NET category on for Beginner level | Views : 35402 red flag
Rating: 5 out of 5  
 1 vote(s)

Here is a very simple example of how to split a string delimited with a particular character or string. I know this is very simple to do but I have find many people struggling in doing that. Attached code also demonstrate how to use StringBuilder, Foreach loop, Regex etc.

 Download source code for Splitting string in C#

Lets start with designing a simple html form with asp.net server controls. Below is the form, I am using to enter string to split and delimited string.

Just enter a long string into the first box and a pattern or delimited string (separated with a characters) like entered into the picture and hit the Submit button.

HTML code for the Form

<form id="form1" runat="server">

<table border="1" style="border-collapse:collapse;border-color:Gray;" cellpadding="2" cellspacing="1">
<th colspan="2" style="background-color:#efefef;">Split String Esxample</th>
<tr align="Right">
<td>String to split:</td>
<td><asp:TextBox runat="Server" id="txtString" Columns="50"></asp:TextBox></td>
<td>Pattern to split with(Delimited string): </td>
<td><asp:TextBox runat="Server" id="txtSplit" Columns="3"></asp:TextBox></td>
<td> </td>
<td><asp:Button ID="btnSubmit" runat="Server" Text="Submit" OnClick="SplitString" /></td>

<br />

<asp:Literal ID="litLabel" runat="server"></asp:Literal>

After hitting the submit button SplitString server side event will fire. Following is the code for it.

Code Behind code

/// <summary>

/// Fires when button is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void SplitString(object sender, EventArgs e)
string str = txtString.Text;
string strSplit = txtSplit.Text;

StringBuilder strB = new StringBuilder("<b>Splitted string: </b><br />", 200);

Regex r = new Regex(strSplit);
string[] s = r.Split(str);

foreach (object o in s)
strB.Append(o.ToString() + "<br />");

litLabel.Text = strB.ToString();

The above code will split the string entered into 1st box where it will find the pattern (matched) character or string entered into 2nd box and write line by line.

Additional Namespace to Use

using System.Text;
using System.Text.RegularExpressions;

Is that a simple one!!!

Don't forget to download the code if you find any problem in understanding above code. The attached code is working fine:). Do write your feedback.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 4/7/2011 | Points: 25
Sir could you please explain why we are using StringBuilder class here ??

I tried the code and it is working absolutely fine....

Thanks and Regards
Posted by: Seenuvasan on: 6/10/2011 | Points: 25
is it possible to use Split() method here?
Posted by: Omniitstudent on: 3/2/2012 | Points: 25
Why we use Cross join in ms sql

Login to post response

Comment using Facebook(Author doesn't get notification)