C# Remove

Remove

Remove eliminates a range of characters. The Remove method on the string class is useful for shortening strings. We look at some examples of using Remove on string. And then we benchmark how it performs versus alternative methods.

These C# examples use the Remove method on the string type. Remove eliminates characters.

Example

Note

Here we see how you can use both overloaded versions of Remove in the C# language. The first section in the code example removes all characters starting at a certain index. The second section removes a range of characters in the middle of an example string.

Program that uses Remove [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// 1. Remove all characters after an index.
	//
	string test1 = "0123456";         // Seven character string.
	string result1 = test1.Remove(3); // Start removing at index 3.
	Console.WriteLine(result1);       // Displays the first three characters.

	//
	// 2. Remove range of characters in string.
	//    See explanation.
	//
	string test2 = "012 345 678";                           // 2A.
	int index1 = test2.IndexOf(' ');                        // 2B.
	int index2 = test2.IndexOf(' ', index1 + 1);            // 2C.
	string result2 = test2.Remove(index1, index2 - index1); // 2D.
	Console.WriteLine(result2);
    }
}

Output

012
012 678
Note

Section 1. The first part of the example removes all characters at and after the third index, which is the character '3' in the string. It displays "012" to the Console. This is how you can shorten or truncate all strings to a certain length.

Truncate String

Section 2. This part shows how you can combine the IndexOf methods with the Remove method. Here we find the index of the first space in 2B. This will be the number 3. Then we find the index of the first space after that, which will be the number 7. Finally, we Remove all characters in that range.

Part 2D: The Remove call here accepts a starting index and a length, not a starting index and ending index. Therefore, you need to subtract the indexes to get the length.

Internals

.NET Framework information

When inspecting the Remove string method in IL Disassembler, I found that the two overloads do very different things. When you use 1 parameter with Remove, you will end up calling "this.Substring(0, startIndex);". Therefore, using one parameter is essentially the same as using Substring.

IL Disassembler Tutorial

Also: For two parameters, the Remove call directly goes to the unmanaged base class library implementation. At this time the exact implementation of this internal method is not public.

Example 2

One common use for Remove is to erase or delete the final character in a string, or erase the first character. These two operations can also be done with Substring. To remove the final character, subtract one from the Length; to remove the first character, use the range of 0, 1.

Program that uses Remove [C#]

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

Replace or Remove?

Question and answer

You can call Replace with an empty replacement string, which will remove substrings. However, this is less exact than using Remove. The two are semantically different. I recommend that you never use Replace to remove certain substrings that always occur in an index range. The Replace calls could end up replacing characters you don't want replaced. Instead use Remove or Substring.

Performance

Performance optimization

Here I benchmarked the Remove method when it removes all characters starting at index 3 of a string. I compared Remove with one parameter; Remove with two parameters; and Substring with two parameters, which does the same thing.

Example string benchmarked [C#]

string source = "012 345 678";
int length = source.Length - 3;

String operations compared, 10000000 iterations [C#]

string s = source.Remove(3);         // <-- Second fastest

string s = source.Remove(3, length); // <-- Slowest

string s = source.Substring(0, 3);   // <-- Fastest

Remove string tests

Remove(a):       236 ms
Remove(a, b):    683 ms [slowest]
Substring(0, a): 222 ms [fastest]

Benchmark results. The results were that using Substring was the fastest. Next the Remove method with one parameter was second fastest. Finally, the Remove method with two parameters was much slower. The times were 236 ms, 683 ms and 222 ms.

Tip: Using Substring was faster than Remove. One parameter overload was faster than two parameters.

Summary

The C# programming language

In this article, we saw several examples of common ways you can use the Remove method on the string type in the C# programming language. We also looked into the internal implementation of Remove on the .NET Framework. Additionally, we benchmarked Remove, and found that it is fastest with one parameter when possible.

String Type
.NET