Home
Search
C# Remove StringUse the Remove method on the string type to eliminate substrings from an input string.
dot net perls
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.
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.
IndexOf
Tip The second argument to Remove() is the count of characters to remove in the string. This argument is optional.
C# program that removes ranges of characters
using System; class Program { static void Main() { // 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.
C# program that erases characters with Remove
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.
TrimEnd, TrimStart
Trim
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); } }
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")); } }
13.18 ns Substring 14.58 ns Remove
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 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.
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 the Remove() method, we delete a range of characters. We can specify 1 or 2 arguments, and a modified string is returned.
Home
© 2007-2021 sam allen. see site info on the changelog