c# - How to append a string to a StringBuilder

Append a String to a StringBuilder
The StringBuilder class represents a mutable String of characters. The StringBuilder class cannot be inherited. The StringBuilder class represents a String-like object whose value is a mutable sequence of characters.

The following .net c# tutorial code demonstrates how we can append a String to a StringBuilder instance. That means how we can add a String object at the end of a StringBuilder instance. In this .net c# tutorial code we will append a String instance to a StringBuilder instance using the StringBuilder class Append() method.

The StringBuilder Append() method appends the String representation of a specified object to this instance. The StringBuilder Append(String) method overload appends a copy of the specified String to this instance.

The StringBuilder Append(string? value) method overload has a parameter named value. The value parameter is the string to append. This method returns a StringBuilder which is a reference to this instance after the append operation has been completed.

The StringBuilder Append(string? value) method overload throws ArgumentOutOfRangeException if enlarging the value of this instance would exceed MaxCapacity.

The Append(string? value) method overload modifies the existing instance of this class. This method does not return a new class instance. Because of this, .net developers can call a method or property on the existing reference and they do not have to assign the return value to a StringBuilder object. If the value parameter value is null, no changes are made and the capacity of this instance is adjusted as needed.

So finally, using the StringBuilder class Append(String) method overload the .net c# developers can append a String to a StringBuilder instance.
stringbuilder-append.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>
  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        StringBuilder stringb = new StringBuilder();
        stringb.Append("this is a string.");
        stringb.Append(" [ ");
        stringb.Append('A');
        stringb.Append(" ] ");
        stringb.Append(". this is another string");

        Label1.Text = stringb.ToString();
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - stringbuilder append</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - stringbuilder append
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <br />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br /><br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="stringbuilder append"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples