Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Using String.Split to split string

Author: DevASP Team
Download Source Code : 709_StringSplitter.zip

In this article you will find a simple trick for splitting the string using string.split to split the string on basis of delimiters.

Here is a simple sample that is used to split the string into array of strings basis on the delimiters. To begin with this drop the two controls a label control and the button control on your web form. The code of your web form is as follows:

 

<asp:Label ID="Label1" runat="server" Text="Label-  Label"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

 

Now on button click event add the following control:

 

    protected void Button1_Click(object sender, EventArgs e)

    {

        char[] ch = { '-' };

        string[] str = Label1.Text.Split(ch);

 

        Label lbl1 = new Label();

        lbl1.Visible = true;

        this.Page.Controls.Add(lbl1);

        lbl1.Text = str[0].ToString();

        Label lbl2 = new Label();

        lbl2.Visible = true;

        this.Page.Controls.Add(lbl2);

        lbl2.Text = str[1].ToString();

    }

 

In above code my delimiter character is hyphen (-) sign. I have added this delimiter to character array and declare the array of string with name str and split the label text using Split(char[] ch). On the web form I have added the two now label controls using Page.Control.Add(Control ctrl).

This property will add new controls on your form. A very simple trick that I am sure will help you a lot in solving your problem.

Article Comments
the above code doesn't work

Posted on 7/30/2009 2:01:31 AM by Udaya

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Programmatically adding Control in GridView Cell (Controls.Add)

Disclaimer - Privacy
© 2002-2017 DevASP.net