C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
IndexOfC# 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
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
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
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
Note: The .NET Framework keeps changing: it is a moving target. So make sure to run the benchmark again.
And: For 2 parameters, the Remove call directly goes to the unmanaged base class library implementation.
Important: Remove and Replace have different semantics (meanings): they represent different behaviors.
Note: The Replace calls could end up replacing characters you don't want replaced. Instead, please use Remove or Substring.