c# - How to replace a character in a string from a starting index

c# example - string replace character at index
string-replace-character-at-index.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        //this line create a string variable.
        string stringValue = "red green blue yellow red snow magenta";

        Label1.Text = "string......................<br />";
        Label1.Text += stringValue;

        string first = stringValue.Remove(22);
        string last = stringValue.Remove(0,22);

        //this line replace 'r' characters with '#' in string after index 22.
        string lastReplaced = last.Replace('r', '#');
        string modifiedString = first.Insert(first.Length, lastReplaced);

        Label1.Text += "<br /><br />splitted string and replaced.....................";
        Label1.Text += "<br />first: " + first;
        Label1.Text += "<br />last: " + last;
        Label1.Text += "<br />replaced: " + lastReplaced;

        Label1.Text += "<br /><br />replaced string (start index 22)......................";
        Label1.Text += "<br />" + modifiedString;
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string replace character at index</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string replace character at index
        </h2>  
        <hr width="550" align="left" color="Gainsboro" />  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="X-Large"
            >  
        </asp:Label>  
        <br /><br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="string replace character at index"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples