TheDeveloperBlog.com


C# Remove Char Examples

Remove chars. Characters can be removed from strings. A char could be at a specific index or must be searched for first. The .NET Framework provides the Remove method on the string type that can help with this problem. We may also require IndexOf.

Remove

Example. First, the Remove method has two overloads that accept one and two parameters. The two parameters are of Int32 type and specify the index at which you want to start removing characters, and then the number of characters to remove.

Tip: To remove a single character, specify the second parameter as the value 1, which indicates a single char.

Here: This example removes the second character, yielding a new string, and then removes the first letter 'A' in another string.

C# program that removes chars

using System;

class Program
{
    static void Main()
    {
	//
	// Remove the second character from the string.
	// This character has the index of 1.
	//
	const string value1 = "Dot Net Perls";
	string result1 = value1.Remove(1, 1);

	//
	// Find and remove the first uppercase A from the string.
	// This character can occur at any index.
	//
	const string value2 = "Sam Allen";
	string result2 = value2;
	int index1 = value2.IndexOf('A');
	if (index1 != -1)
	{
	    result2 = value2.Remove(index1, 1); // Use integer from IndexOf.
	}

	//
	// Output of the program.
	//
	Console.WriteLine(result1);
	Console.WriteLine(result2);
    }
}

Output

Dt Net Perls         (Second character was removed.)
Sam llen             (Letter A was removed.)

The first call to Remove targets the second character and continues removing for only one character. This call returns the string "Dt Net Perls", with the "Dot" having its middle character removed.

The next part of this example is more useful in many programs. It combines the IndexOf method, which returns the index of the first uppercase letter 'A'. If no such character is found, the IndexOf method will return -1.

It is usually important that you check the result of IndexOf against -1. Inside the if-statement, the Remove method is called with the index integer found. This yields a string that no longer has the first 'A' character found.


Example 2. Chars can be removed by value. This can be done with a custom loop method or with the string Replace method. Here we show some method implementations for removing all double-quote characters.

RemoveQuotes1 allocates a char array. It then fills this array with all non-quote characters from the source string. It keeps track of the length of the result, and then uses the string constructor to create the result.

And: The second method, RemoveQuotes2, is much shorter and simply replaces the string "\"" with the empty string literal.

Method that removes char by value with loop: C#

static string RemoveQuotes1(string input)
{
    int index = 0;
    char[] result = new char[input.Length];
    for (int i = 0; i < input.Length; i++)
    {
	if (input[i] != '"')
	{
	    result[index++] = input[i];
	}
    }
    return new string(result, 0, index);
}

Method that removes char by value with Replace: C#

static string RemoveQuotes2(string input)
{
    return input.Replace("\"", "");
}

Benchmark details
    These statements were used in tight loops.

RemoveQuotes1("Thanks for \"visiting\"!");
RemoveQuotes2("Thanks for \"visiting\"!");

Benchmark results
    RemoveQuotes1 was much faster.

105.75 ns
240.84 ns

The first method, RemoveQuotes1, was much faster. This is because we provided a custom implementation and could act upon individual characters and not strings. RemoveQuotes1 is longer and more prone to implementation mistakes.


Replace chars. The Replace instance method on the string type has an overload that accepts two char parameters. You cannot replace all chars of one value with an empty char with this method. This is because there is no such thing as an empty char.

Instead: If you want to Replace characters, specify the characters as strings and use an empty string literal as the second parameter.

Replace

Substring. Let's review the Substring and Remove methods. Both of these methods provide a way to take a range of characters from a string and use them to fill a new character data buffer and provide a new string reference to that.

Substring

Tip: In this example, you could use Substring instead of Remove with no functional difference.


Char arrays are one of the best string manipulation optimizations you can use. They often reduce memory allocations and reduce instructions due to less copying. I often use char arrays for performance.

Char Array

Summary. We removed a specific character from a string. You can use this code to remove either a character at any index. The code invokes the Remove method, the Substring method (internally), and the IndexOf method.

Also: We noted issues with the Replace method on chars and reviewed related resources.