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

April 11, 2014 0 Comments

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>

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();


Jackrin Reacher

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google