c# - How to convert a StringBuilder to a string

Convert a StringBuilder to a String
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.

Although StringBuilder and String both represent sequences of characters, they are implemented differently. The String is an immutable type. So, each operation that appears to modify a String object actually creates a new String.

The following .net c# tutorial code demonstrates how we can convert a StringBuilder instance to a String object. In this .net c# tutorial code we will convert a StringBuilder instance to a String object by using the StringBuilder ToString() method.

The StringBuilder ToString() method converts the value of a StringBuilder to a String. The StringBuilder ToString() method returns a String whose value is the same as this instance.
convert-stringbuilder-to-string.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(" Crimson");

        //insert a boolean value at index 0.
        stringb.Insert(0, false);
        stringb.Insert(0, " || "); //insert a separator here

        //insert a char value at index 0.
        stringb.Insert(0, 'D');
        stringb.Insert(0, " || "); //insert a separator here

        //insert a double value at index 0.
        stringb.Insert(0, 21.36);
        stringb.Insert(0, " || "); //insert a separator here

        //insert a float value at index 0.
        stringb.Insert(0, 4.88f);
        stringb.Insert(0, " || "); //insert a separator here

        //insert a int value at index 0.
        stringb.Insert(0, 85);
        stringb.Insert(0, " || "); //insert a separator here

        //insert a string value at index 0.
        stringb.Insert(0, "yellow");

        //insert a string value at index 6.
        stringb.Insert(6, ":::::");
        
        //this is the final string which we get from stringbuilder.
        string finalstring = stringb.ToString();

        Label1.Text = finalstring;
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - convert stringbuilder to string</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - convert stringbuilder to string
        </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="convert stringbuilder to string"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples