Home
Map
String RemoveUse the Remove method on the string type to eliminate substrings from an input string.
C#
This page was last reviewed on Mar 30, 2023.
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.
String Insert
Method notes. 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.
Version 1 In this code we remove all characters at (and after) the index 3, which is the character "3" in the string.
Version 2 Here we remove a range of characters in the middle of a string. We call IndexOf, and pass 2 arguments to remove.
String IndexOf
Tip The second argument to 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.
Part 1 To remove the final character, subtract one from the string length. We can access the Length property.
String Length
Part 2 To remove the first character, we begin removing at index 0, and continue for 1 character.
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, 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.
String TrimEnd, TrimStart
String Trim
Tip If you are using 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
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.
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
Discussion. We can call Replace with an empty replacement string, which will remove substrings. However this is less exact than using Remove.
String Replace
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.
Special methods. 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.
String Truncate
String Remove Char
A summary. With the Remove() method, we delete a range of characters. We can specify 1 or 2 arguments, and a modified string is returned.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 30, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.