c# - How to append a newline to a StringBuilder

c# example - stringbuilder append newline
The following asp.net c# example code demonstrate us how can we add/append new line to a StringBuilder object programmatically at run time in an asp.net application. .Net framework's StringBuilder Class represent a mutable string of characters. String Builder Class has no built in property or method to append new line to it.

StringBuilder Class StringBuilder.Append(String) overloaded method allow us to append a copy of the specified string to this instance. So, we can append a string (sequence of characters) to a StringBuilder object programmatically by using this Append() method.

.Net framework's System.Environment.NewLine property allow us to get the new line string defined for this environment. So, we can insert a new line to a StringBuilder object by using this System.Environment.NewLine property. This property return a string containing "\r\n" for non-Unix platforms or a string containing "\n" for Unix platforms. The return string is the new line separator characters for specified platform.

For example, we can append a new line to a StringBuilder object as StringBuilder.Append(System.Environment.NewLine). This property append a environment specified new line string to the StringBuilder object. Finally, we get a StringBuilder object which generate new line on specified position.

In the following example code, we also save the StringBuilder object to a text file on web server file system. The text file clearly shows the generated new line on it.
stringbuilder-append-newline.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.IO" %>
  
<!DOCTYPE html>
  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        StringBuilder stringb = new StringBuilder();
        stringb.Append("magenta");
        //this line append (insert) new line in stringbuilder
        stringb.AppendLine();

        stringb.Append("maroon");
        stringb.AppendLine();

        stringb.Append("pink");
        stringb.AppendLine();
        //another way to append new line in stringbuilder
        stringb.Append(System.Environment.NewLine);
        stringb.Append("darkblue.");

        Label1.Text = stringb.ToString();

        string appPath = Request.PhysicalApplicationPath;
        string filePath = appPath + "newlinetest.txt";  

        using (StreamWriter swriter = new StreamWriter(filePath))
        {
            swriter.Write(stringb.ToString());
        }        
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - stringbuilder append newline</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - stringbuilder append newline
        </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 newline"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>

More c# examples