Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 16716 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Splitting string in C#

Splitting string in C#

1 vote(s)
Rating: 5 out of 5
Article posted by SheoNarayan on 10/1/2007 | Views: 22127 | Category: ASP.NET | Level: Beginner red flag


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


 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">

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

<br />


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


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.

Regards

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

 Responses
Posted by: Akiii | Posted on: 07 Apr 2011 10:53:20 AM | 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
Akiii

Posted by: Seenuvasan | Posted on: 10 Jun 2011 09:02:49 AM | Points: 25

is it possible to use Split() method here?

Posted by: Omniitstudent | Posted on: 02 Mar 2012 10:02:58 AM | Points: 25

Why we use Cross join in ms sql

>> Write Response - Respond to this post and get points
Related Posts

This article describes how to change background colour of the whole item of a list view based on the search string

Many times we want certain set of validation to fire for some user and certain set of validation to fire for some other user. In the section we will see how we can achieve the same using Microsoft enterprise validation blocks.

In asp.net the FileUpload Field doesnot provied a way to clear its input the conventional way other controls do.

This article explains how to display mouseover effect in GridView rows using only CSS. In this article, we are not going to use jQuery or JavaScript.

There are several ways to populate DropDownList control. In this article I have tried to cover some of them including populating DropDownList manually, programmatically, through xml, database, arraylist, hashtable. I have also covered how to specify different background color of every items of the DropDownList and how to generate DropDownList on the fly. At last I have covered how to get its index, text and value properties.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 8:56:39 AM