Remove
charsIn C# programs, characters can be removed from strings. A char
could be at a specific index or must be searched for first.
In .NET, we have the Remove()
method on the string
type. Even though it accepts string
arguments, this method can help with removing characters.
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.
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);AC ABC BC ABC
char
removalChars can be removed by value. This can be done with a custom loop method or with the string
Replace
method.
double
-quote characters.RemoveQuotes1
allocates a char
array. It creates a string
from all non-quote characters from the source string
.RemoveQuotes2
, is much shorter and simply replaces a quotation mark with the empty string
literal.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
charsReplace()
has an overload that accepts 2 char
parameters. You cannot replace all chars of one value with an empty char
with this method.
char
.Replace
characters, specify the characters as strings and use an empty string
literal as the second parameter.Let's review Substring
and Remove
. In this example, you could use Substring
instead of Remove
with no functional difference.
Char
arraysChar
arrays are one of the best string
manipulation optimizations you can use. They often reduce memory allocations and reduce instructions due to less copying.
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
.