c# - How to remove a character from a string at specified position

String remove character
The following asp.net c# example code demonstrate us how can we remove a specified character from a string object programmatically at run time in an asp.net application. .Net framework's String Class String.Remove(Inte32, Int32) overloaded method allow us to remove specified number of characters at starting a specified index position from a string instance.

So, if we want to remove a specified character from a specified index position of a string, we need to set the number of characters to remove to 1 (one). And we also need to pass the index value of the specified character to Remove() method.

Such as, if we want to remove the 5th character of a string, we should pass the parameters to Remove() method as Remove(4,1), because string contain zero-based index. Here, first parameter represent the index of specified character to begin removing and second parameter represent the number of characters to remove from beginning index position.

Finally, we can call the Remove() method to remove a single character from a string object as String.Remove(SpecifiedCharacterIndex, 1).

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

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create a string variable.
        string plants = "Broadleaf. False Boxwood. Brown Betty.";

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

        //remove second character only from string
        string without2ndCharacter = plants.Remove(1, 1);

        //remove 5th character only from string
        string without5thCharacter = plants.Remove(4, 1);

        Label1.Text += "<br />string remove second character: " + without2ndCharacter;
        Label1.Text += "<br />string remove 5th character: " + without5thCharacter;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string remove character</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string remove character
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string remove character"  
More c# examples