c# - How to append a Tab to a StringBuilder

c# example - stringbuilder append tab
The following asp.net c# example code demonstrate us how can we add/append a Tab (tabulation) inside a StringBuilder characters programmatically at run time in an asp.net application. In .net framework's StringBuilder Class represent a mutable string of characters.

StringBuilder.Append(String) overloaded method allow us to append a copy of the specified string to this StringBuilder instance. Append(String) method require to pass a parameter named 'value' which value data type is System.String. This string value is the string to append to StringBuilder object.

StringBuilder.Append() method return the modified StringBuilder object which is a reference to this instance after the append operation has completed.

To insert a Tab (tabulation) inside a String, we can put a '\t' in the specified position of this string. So, if our StringBuilder object has two words 'Sample' and 'Text', and we want to append a Tab between the two words then we can follow this procedure.

First, we append the word 'Sample' to the StringBuilder object as StringBuilder.Append("Sample"). Next, we append a Tab to StringBuilder object as StringBuilder.Append("\t") and at last we need to add/append the word 'Text' to StringBuilder object. Finally, we will get a StringBuilder object which have a Tab between two words.

In the following example code, we also save the StringBuilder object to a text file in web server file system. The bellow images shows the output of this tutorial.
stringbuilder-append-tab.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("red");
        //this line insert a tab (tabulation) in stringbuilder
        stringb.Append("\t");
        stringb.Append("green");
        stringb.Append("\t");
        stringb.Append("blue");
        stringb.Append("\t");
        stringb.Append("yellow.");

        Label1.Text = stringb.ToString();

        string appPath = Request.PhysicalApplicationPath;
        string filePath = appPath + "tabtest.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 tab</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - stringbuilder append tab
        </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 tab"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>


More c# examples