TheDeveloperBlog.com


C# Replace Extension

Replace searches every character for the pattern. This is true even if a replacement has already occurred. With a custom extension method we optimize Replace and only replace the first matching pattern.


Example. Let's get started with this simple program. We introduce the ReplaceFirst extension method in the Extensions class. Inside ReplaceFirst, we call IndexOf to find the first matching pattern.

Then: We create a char array and call CopyTo to write the parts of the final string to the array. We return the finished string.

CopyToString Constructor
C# program that uses custom ReplaceFirst method

using System;
using System.Diagnostics;

static class Extensions
{
    /// <summary>
    /// ReplaceFirst.
    /// </summary>
    public static string ReplaceFirst(this string value, string pattern, string replacement)
    {
	// Get index.
	int index = value.IndexOf(pattern);

	// See if -1.
	if (index == -1)
	{
	    return value;
	}

	// Lengths.
	int replacementLength = replacement.Length;
	int patternLength = pattern.Length;
	int valueLength = value.Length;

	// Build arrays.
	char[] array = new char[valueLength + replacementLength - patternLength];
	value.CopyTo(0,
	    array,
	    0,
	    index);
	replacement.CopyTo(0,
	    array,
	    index,
	    replacementLength);
	value.CopyTo(index + patternLength,
	    array,
	    index + replacementLength,
	    valueLength - (index + patternLength));
	return new string(array);
    }
}

class Program
{
    static void Main()
    {
	const string value = "testtesttest$1test1234567890123456789";
	Console.WriteLine(value.ReplaceFirst("$1", "test"));
	Console.WriteLine(value.ReplaceFirst("$1", ""));
	Console.WriteLine("cat".ReplaceFirst("a", ""));
	Console.WriteLine("perl".ReplaceFirst("x", "y"));

	const int m = 10000000;
	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    string a = value.ReplaceFirst("$1", "test");
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	{
	    string a = value.Replace("$1", "test");
	}
	s2.Stop();
	Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
	    m).ToString("0.00 ns"));
	Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
	    m).ToString("0.00 ns"));
	Console.Read();
    }
}

Result

testtesttesttesttest1234567890123456789
testtesttesttest1234567890123456789
ct
perl
229.47 ns
244.37 ns

From the limited set of tests in the program, we see that the ReplaceFirst method works correctly at least in some situations. It is faster than the regular Replace method in the test case.

Note: This is partly because it can stop searching for the value to Replace after a replacement occurred.

However: Implementation differences may better explain the performance difference.


Summary. Is creating this sort of method a good use of your time? The performance change here is not important. If it is critical that only the first pattern in the input string be replaced, then this sort of method could be useful.