TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Remove String Method

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

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. And then we benchmark how it performs versus alternative methods.

Example. We use both overloaded versions of Remove. 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.

Overload

First: The first part of the example removes all characters at and after the third index, which is the character "3" in the string.

Truncate String

IndexOf: The second part combines IndexOf with Remove. We find the index of the first and second spaces, then Remove that range.

Based on:

.NET 4.5

C# 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

Internals. Internally the two overloads do different things. When you use one 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

And: For two parameters, the Remove call directly goes to the unmanaged base class library implementation.

Example 2. One common use for Remove is to erase or delete the final character in a string, or erase the first character. To remove the final character, subtract one from the Length. To remove the first character, use the range of 0, 1.

Program 2: 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

Discussion. 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—they represent different behaviors.

Tip: I recommend that you never use Replace to remove certain substrings that always occur in an index range.

Caution: The Replace calls could end up replacing characters you don't want replaced. Instead, please use Remove or Substring.

Performance. Here I benchmarked the Remove method when it removes all characters starting at index 3 of a string. I compared Remove with one and two parameters. I tested also 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

Results

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

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.

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

Summary. With this method, we delete a range of characters. We saw examples of common ways to use this string method. And we looked into the internal implementation of Remove in the .NET Framework.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf