c# - How to create bold text inside a StringBuilder

c# example - stringbuilder bold text
The following asp.net c# example code demonstrate us how can we render a part of text to bold characters inside a StringBuilder object programmatically at run time in an asp.net application.

.Net framework's StringBuilder Class represent a mutable string of characters. Each operation that appears to modify a StringBuilder object does not create a new StringBuilder object, it just return itself as modified StringBuilder object.

.Net framework's StringBuilder Class has no built in method or property to render part of text/characters are bold inside a StringBuilder object. So, we can technically render bold text inside a StringBuilder sequence of characters.

StringBuilder Class StringBuilder.Append(String) overloaded method allow us to append a copy of the specified string to this StringBuilder instance. We can populate a StringBuilder object by characters using this Append() method. To render a part of text as bold text we can append HTML '<b>Text</b>' (Bold) tag outside of the specified text (sequence of characters).

For example, if our StringBuilder text is 'This is a sample text' and we want to bold the word 'sample', then we can add HTML bold tag to start and end of the word 'sample' as '<b>sample</b>'. We append the bold tag by using StringBuilder Append() method.
stringbuilder-bold-text.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("Planalto Hermit.");
        stringb.AppendLine();

        stringb.Append("Violet Sabrewing.");
        stringb.AppendLine();

        //this section create a bold text in stringbuilder.
        stringb.Append("<b>");
        stringb.Append("Alpine Swift");
        stringb.Append("</b>");
        stringb.Append('.');
        stringb.AppendLine();

        stringb.Append("Ecuadorian Hillstar");
        stringb.AppendLine();

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