c# - How to replace a string in a StringBuilder

Replace a String in a StringBuilder
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.

The following .net c# tutorial code demonstrates how we can replace a String with another specified String in a StringBuilder instance. That means we will replace all occurrences of a String with another specified String in the StringBuilder object. In this .net c# tutorial code we will use the StringBuilder class Replace() method to replace a String with another specified String in a StringBuilder object.

The StringBuilder Replace() method replaces all occurrences of a specified character or String in this instance with another specified character or String. The StringBuilder Replace(String, String) method overload replaces all occurrences of a specified String in this instance with another specified String. So, using this method we can replace all the occurrences of a String with another specified String in a StringBuilder object.

The StringBuilder Replace(string oldValue, string? newValue) method overload has two parameters those are oldValue and newValue. The oldValue is the String to replace and the newValue is the String that replaces the oldValue or null. This method returns a StringBuilder which is a reference to this instance with all instances of oldValue replaced by newValue.

The StringBuilder Replace(String, String) method overload throws ArgumentNullException if the oldValue is null. It also throws ArgumentException if the length of oldValue is zero. The method throws ArgumentOutOfRangeException when enlarging the value of this instance would exceed MaxCapacity.

So finally, using the StringBuilder class Replace(String, String) method overload the .net c# developers can replace all the occurrences of a String with another specified String in a StringBuilder instance.
stringbuilder-replace.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("Black Bisque White Chocolate Coral");

        Label1.Text = stringb.ToString();

        stringb.Replace("White", "Snow");

        Label1.Text += "<br /><br />After replace 'White' to 'Snow'......<br />";
        Label1.Text += stringb.ToString();

        stringb.Replace("Coral", "Crimson");

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