How to split the string


Introduction: Hello friends, in this article i will explain that how we can split the string from a particular word. Here i’ll split the string from the <br/>tag.

Implementation: Create a new website add a page named split.aspx. Drag and drop a label named lbl_display and a button named btn_split from the toolbox at .aspx page.

Code for split.aspx page:

<form id="form1" runat="server">
    <div>
        <asp:Button ID="btn_split" runat="server" Text="Show Data"
            onclick="btn_split_Click" /><br />
        <asp:Label ID="lbl_display" runat="server" Text=""></asp:Label>
    </div>
    </form>

Code for gridview.aspx.cs page:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class split : System.Web.UI.Page
{
    // here m declaring StringBuilder
    StringBuilder str_split = new StringBuilder();
    // These variable are for print solution in  next line
    string[] parts1;
    string[] parts2;
    string output;
    string input;
    Int32 k;
    Int32 j;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            // here i am addding the data inside in stringbuilder
            str_split.Append(@"Lorem Ipsum is simply dummy text of the printing and typesetting industry.<br/>
            Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, <br/>
            when an unknown printer took a galley of type and scrambled it to make a type specimen book.<br/>
            It has survived not only five centuries, but also the leap into electronic typesetting,
            remaining essentially unchanged.<br/>
            It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, <br/>
            and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
            // here here m adding  the stringbulder's data inside the input sting
            input = str_split.ToString();
            // here i am splitting the data after every <br/>
            parts1 = input.Split(new string[] { "<br/>" }, StringSplitOptions.None);
            // here i am using for loop to insert the data inside the  ViewState["parts"]
            //from the array  parts1 and add <br/> after every line
            for (k = 0; k < 6; k++)
            {
                ViewState["parts"] += (parts1[k] + "<br/>");
            }
            // here m using a viewstate to displaying the the data after clicking the button
            ViewState["value"] = 0;
        }
    }
    protected void btn_split_Click(object sender, EventArgs e)
    {

       // here m emptying the label after every click
        lbl_display.Text = string.Empty;
        //  // here here m adding  the ViewState's data inside the output sting
        output = ViewState["parts"].ToString();
        // here i am splitting the data after every <br/>
        parts2 = output.Split(new string[] { "<br/>" }, StringSplitOptions.None);
        Int32 j;
        // here m using the for loop to display the data step by step inside teh label
        for (j = 0; j <= Convert.ToInt32(ViewState["value"]); j++)
        {
            lbl_display.Text += (parts2[j] + "<br/>");
        }
        // when all line will be printed then the below condition will be satisfaies.
        // and the code will enter inside this condition
        if (Convert.ToInt32(ViewState["value"]) == 6)
        {
            // here m disabling the button after printing all the line
            btn_split.Enabled = false;
          
            //ViewState["value"]= "";
            return;
        }
        // here m incrementing in the viewstate with 1 everytime
        ViewState["value"] = Convert.ToInt32(ViewState["value"]) + 1;

      
    }
}


Below i am displaying the output of this program in the image after first click:



Below i am displaying the output of this program in the image after second click:



Below i am displaying the output of this program in the image after third click:


Below i am displaying the output of this program in the image after fourth click:



Below i am displaying the output of this program in the image after fifth click:


Below i am displaying the output of this program in the image after sixth click:


After sixth click of the button, button will be disable because after sixth click all the complete six lines will be display with the label on your screen.

Conclusion: Dear friends, through this article you have learned that how we can split the string.  



Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net