-->

Friday, April 11, 2014

How to remove last character from the string in ASP.NET

How to remove last character from the string in ASP.NET

If you want to remove last character from the string, you can use Remove method of the StringBuilder class.
In this method, you must specify the length of the string also specify how many character remove from the string. Lets take an example , we take a StringBuilder class object, append this object with some text. After assigning some string value into object, you can remove characters from the string object.
<form id="form1" runat="server">
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click"
            Width="80px" />
   
    <asp:Label ID="Label1" runat="server" BackColor="#FFFF66" Text="Label"></asp:Label>
    </form>

Code behind code-


 protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder stringA = new StringBuilder();
        stringA.Append("it is a text. ");
        stringA.Append("it is another text.");

        Label1.Text = stringA.ToString();

        stringA.Remove(stringA.Length - 1, 1);

        Label1.Text += "<br /><br />after removing last character from stringbuilder....<br />";
        Label1.Text += stringA.ToString();
    }


Output-



Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved