HomeSearch

C# Remove Char From String at Index

Remove single characters from strings. A char can be removed at an index.
Remove chars. Characters can be removed from strings. A char could be at a specific index or must be searched for first. The .NET Framework provides the Remove method on the string type that can help with this problem.StringsChar
Example. The Remove method has 2 overloads that accept 1 and 2 parameters. The parameters are of Int32 type and specify the index at which you want to start removing characters, and then the number of characters to remove.Remove

Tip: To remove a single character, specify the second parameter as the value 1, which indicates a single char.

C# program that removes chars using System; class Program { static void Main() { // // Remove the second character from the string. // This character has the index of 1. // const string value1 = "ABC ABC"; string result1 = value1.Remove(1, 1); // // Find and remove the first uppercase A from the string. // This character can occur at any index. // const string value2 = "ABC ABC"; string result2 = value2; int index1 = value2.IndexOf('A'); if (index1 != -1) { result2 = value2.Remove(index1, 1); } Console.WriteLine(result1); Console.WriteLine(result2); } } Output AC ABC BC ABC
Example 2. Chars can be removed by value. This can be done with a custom loop method or with the string Replace method. Here we show some method implementations for removing all double-quote characters.

First: RemoveQuotes1 allocates a char array. It creates a string from all non-quote characters from the source string.

Second: The second method, RemoveQuotes2, is much shorter and simply replaces a quotation mark with the empty string literal.

Result: The first method, RemoveQuotes1, was much faster. It can act upon individual characters and not strings.

Method that removes char by value with loop: C# static string RemoveQuotes1(string input) { int index = 0; char[] result = new char[input.Length]; for (int i = 0; i < input.Length; i++) { if (input[i] != '"') { result[index++] = input[i]; } } return new string(result, 0, index); } Method that removes char by value with Replace: C# static string RemoveQuotes2(string input) { return input.Replace("\"", ""); } Benchmark details RemoveQuotes1("Thanks for \"visiting\"!"); RemoveQuotes2("Thanks for \"visiting\"!"); Benchmark results 105.75 ns 240.84 ns
Replace chars. The Replace instance method on the string type has an overload that accepts two char parameters. You cannot replace all chars of one value with an empty char with this method. This is because there is no such thing as an empty char.

Instead: If you want to Replace characters, specify the characters as strings and use an empty string literal as the second parameter.

Replace
Substring. Let's review the Substring and Remove methods. Both of these methods provide a way to take a range of characters from a string and use them to fill a new character data buffer and provide a new string reference to that.Substring

Tip: In this example, you could use Substring instead of Remove with no functional difference.

Char arrays. Char arrays are one of the best string manipulation optimizations you can use. They often reduce memory allocations and reduce instructions due to less copying.Char Array
Summary. We removed a specific character from a string. You can use this code to remove either a character at any index. The code invokes the Remove method, the Substring method (internally), and the IndexOf method.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls