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).
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; public class Program { public static void Main(string[] args) { string plants = "Broadleaf. False Boxwood. Brown Betty."; string without2ndCharacter = plants.Remove(1, 1); //remove 5th character only from string string without5thCharacter = plants.Remove(4, 1); Console.WriteLine("string remove second character: " + without2ndCharacter); Console.WriteLine("string remove 5th character: " + without5thCharacter); } } |
Output: