Remove
This C# string
method eliminates a range of characters. It is useful for shortening strings. It creates a new string
when it is called.
This method can often be used alongside other methods like IndexOf
. We review examples of using Remove
with 1 or 2 arguments.
In this example, we call Remove()
in 2 different ways. The first argument is an int
that indicates where the method should start removing characters—a start index.
string
. We call IndexOf
, and pass 2 arguments to remove.Remove()
is the count of characters to remove in the string. This argument is optional.using System; // Version 1: use 1 argument with Remove. // ... Remove all characters after an index. string test1 = "0123456"; string result1 = test1.Remove(3); Console.WriteLine(result1); // Version 2: use 2 arguments with Remove. // ... Remove range of characters in string. 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);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.
string
length. We can access the Length
property.using System; class Program { static void Main() { // Part 1: remove the last character in a string. string test1 = "0123456"; test1 = test1.Remove(test1.Length - 1); Console.WriteLine(test1); // Part 2: remove the first character in a string. string test2 = "0123456"; test2 = test2.Remove(0, 1); Console.WriteLine(test2); } }012345 123456
Trim
, removeOther 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.
Remove()
in place of a method like TrimStart
, it might be clearer to use TrimStart
.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); } }TRIMMED STRING: bird REMOVED STRING: bird
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.
Substring
to take only the first 3 characters of the source string
.Remove()
to eliminate all except the first 3 characters of the source string
.Remove()
method is slower than Substring()
, as more logic to adjust the arguments is present in the .NET Framework.using System; using System.Diagnostics; const int _max = 100000000; 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"));13.18 ns Substring 14.58 ns Remove
We can call Replace
with an empty replacement string
, which will remove substrings. However this is less exact than using Remove
.
Remove
and Replace
have different semantics (meanings): they represent different behaviors.Replace
calls could end up replacing characters you don't want replaced. Instead, please use Remove
or Substring
.We can develop a string
truncation method that has some internal logic to help us remove string
parts easier. We can remove a single char
from a string
.
With the Remove()
method, we delete a range of characters. We can specify 1 or 2 arguments, and a modified string
is returned.