String Remove Char Example
This page was last reviewed on Oct 3, 2023.
Dot Net Perls
Remove chars. In C# programs, characters can be removed from strings. A char could be at a specific index or must be searched for first.
Method info. In .NET, we have the Remove() method on the string type. Even though it accepts string arguments, this method can help with removing characters.
String Remove
Example. Remove() accepts 2 parameters. The parameters specify the index at which you want to start removing characters, and then the number of characters to remove.
Tip To remove a single character, specify the second parameter as the value 1, which indicates a single char.
using System; // 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);
Benchmark, char removal. 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.
Version 1 RemoveQuotes1 allocates a char array. It creates a string from all non-quote characters from the source string.
Version 2 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.
using System; using System.Diagnostics; class Program { 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); } static string RemoveQuotes2(string input) { return input.Replace("\"", ""); } const int _max = 1000000; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: optimized method. for (int i = 0; i < _max; i++) { RemoveQuotes1("Thanks for \"visiting\"!"); } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: replace call. for (int i = 0; i < _max; i++) { RemoveQuotes2("Thanks for \"visiting\"!"); } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } }
40.66 ns 72.67 ns
Replace chars. Replace() has an overload that accepts 2 char parameters. You cannot replace all chars of one value with an empty char with this method.
Info We cannot use this overload because there is no such thing as an empty char.
Detail If you want to Replace characters, specify the characters as strings and use an empty string literal as the second parameter.
String Replace
String Change Characters
Note, substring. Let's review Substring and Remove. In this example, you could use Substring instead of Remove with no functional difference.
String Substring
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
A summary. We removed a specific character from a string. You can use this code to remove either a character at any index. The code uses Remove, Substring (internally), and IndexOf.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 3, 2023 (edit).
© 2007-2024 Sam Allen.