c# - How to loop through a StringBuilder elements

Loop through a StringBuilder elements
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 loop through a StringBuilder object’s elements. So, in this .net c# tutorial code we will iterate all of the elements of a StringBuilder instance. Here we will count elements of a StringBuilder object those are separated by white space. That means we will loop through the words or characters that are separated with white space in a StringBuilder instance.

There are no direct methods in the StringBuilder class to loop through its elements. So at first, we have to convert the StringBuilder object to a String object. The StringBuilder ToString() method converts the value of a StringBuilder to a String.

Then we will split the String object into an Array by white space/empty space/blank space. Finally, we loop through the converted Array elements and display the items on the user interface. The Array elements are actually the elements of the StringBuilder instance.

The String Split() method returns a String Array that contains the substrings in this instance that are delimited by elements of a specified String or Unicode character Array.

The String Split(Char, StringSplitOptions) method overload splits a String into substrings based on a specified delimiting character and optionally String split options. So we can use this method to split a String object by white space.
loop-through-stringbuilder.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("red green blue yellow brown olive pink");

        Label1.Text = stringb.ToString();
        Label1.Text += "<br /><br />loop through stringbuilder using space separator.....<br />";

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