HomeSearch

C# Remove String

Use the Remove method on the string type to eliminate substrings from an input string.
Remove. This string method eliminates a range of characters. It is useful for shortening strings. It creates a new string when it is called.Strings
This method can often be used alongside other methods like IndexOf. We review examples of using Remove. And then we benchmark how it performs versus alternative methods.
An example. First we remove all characters at (and after) the index 3, which is the character "3" in the string. Then we remove a range of characters in the middle of a string.

Argument 1: The first argument is an int that indicates where the method should start removing characters—a start index.

Argument 2: This is the count of characters to remove in the string (if not specified, every following char is removed).

IndexOf: We can combine IndexOf with Remove. We find the index of the first and second spaces, then Remove that range.

IndexOf
C# program that uses Remove using System; class Program { static void Main() { // // 1. Remove all characters after an index. // // ... Seven character string. string test1 = "0123456"; // ... Start removing at index 3. string result1 = test1.Remove(3); // ... Displays the first three characters. Console.WriteLine(result1); // // 2. Remove range of characters in string. // See explanation. // string test2 = "012 345 678"; int index1 = test2.IndexOf(' '); int index2 = test2.IndexOf(' ', index1 + 1); string result2 = test2.Remove(index1, index2 - index1); Console.WriteLine(result2); } } Output 012 012 678
Remove char. One use for Remove is to erase or delete the final character in a string, or erase the first character. Remove() acts on strings, but it can remove a single character too.

Tip: To remove the final character, subtract one from the Length. To remove the first character, use the range of 0, 1.

C# program that erases characters with Remove using System; class Program { static void Main() { // // Remove the last character in a string. // string test1 = "0123456"; test1 = test1.Remove(test1.Length - 1); Console.WriteLine(test1); // // Remove the first character in a string. // string test2 = "0123456"; test2 = test2.Remove(0, 1); Console.WriteLine(test2); } } Output 012345 123456
Trim, remove. Other string methods (like TrimStart, TrimEnd, or Trim) can also remove certain characters in a string. The effect is sometimes the same as calling Remove ourselves.TrimEnd, TrimStartTrim

Tip: If you are using Remove() in place of a method like TrimStart, it might be clearer to use TrimStart.

C# program that uses TrimStart, Remove using System; class Program { static void Main() { string test = "aaabird"; // Remove chars at the start with TrimStart and Remove. // ... These methods do different things, but sometimes can be exchanged. string trimmedString = test.TrimStart(new char[] { 'a' }); string removedString = test.Remove(0, 3); Console.WriteLine("TRIMMED STRING: " + trimmedString); Console.WriteLine("REMOVED STRING: " + removedString); } } Output TRIMMED STRING: bird REMOVED STRING: bird
Performance. Here we benchmarked the Remove method against an equivalent Substring call. Remove() internally calls substring, so this tests the overhead of the Remove-specific logic.

Version 1: This code calls Substring to take only the first 3 characters of the source string.

Version 2: This version of the code uses Remove() to eliminate all except the first 3 characters of the source string.

Result: The Remove() method is slower than Substring(), as more logic to adjust the arguments is present in the .NET Framework.

C# program that benchmarks Remove, Substring using System; using System.Diagnostics; class Program { const int _max = 100000000; static void Main() { string source = "012 345 678"; int length = source.Length - 3; var s1 = Stopwatch.StartNew(); // Version 1: use Substring. for (int i = 0; i < _max; i++) { string result = source.Substring(0, 3); if (result == null) { return; } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use Remove. for (int i = 0; i < _max; i++) { string result = source.Remove(3, length); if (result == null) { return; } } 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")); } } Output 13.18 ns Substring 14.58 ns Remove
Results, benchmark. Using Substring is faster than Remove. But the ideal approach is to keep programs readable, so using Remove() if it makes more sense (for humans) is a good idea.

Note: The .NET Framework keeps changing: it is a moving target. So make sure to run the benchmark again.

Internals, remove. The 2 overloads do different things. With 1 parameter, you will end up calling "this.Substring(0, startIndex)". This is essentially the same as using Substring.IL Disassembler

And: For 2 parameters, the Remove call directly goes to the unmanaged base class library implementation.

Discussion. You can call Replace with an empty replacement string, which will remove substrings. However this is less exact than using Remove.Replace

Important: Remove and Replace are semantically different—they represent different behaviors.

Note: The Replace calls could end up replacing characters you don't want replaced. Instead, please use Remove or Substring.

Truncate. We can develop a special "string truncation" method that has some internal logic to help us remove string parts easier. This is better than duplicating code.Truncate String
A summary. With this method, we delete a range of characters. We looked into the internal implementation of Remove in the .NET Framework.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls