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).
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).
string-remove-character.aspx
<%@ 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;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string remove character</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string remove character
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="string remove character"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to remove a substring from a string
- How to remove the last character from a string
- How to remove white spaces from a string
- How to remove non numeric characters from a string
- How to reverse a string
- How to get number of characters from left side of a string
- How to trim the last character of a string
- How to truncate a string
- How to trim a string to a specified length
- How to convert a string to a datetime object