C# Replace Extension

String type

When you call the Replace method on a string, every character is searched for the pattern, even if a replacement has already occurred. With a custom extension method, as shown here, we can optimize Replace and only replace the first matching pattern.

Replace String Examples

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 character array and call CopyTo to write the parts of the final string to the array. We call the string constructor and return the finished string.

IndexOf String Examples CopyTo String Method String Constructor

This C# example program implements a custom Replace method. It only replaces one instance of the substring.

Program that uses custom ReplaceFirst method [C#]

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

Overview. From the limited set of tests in the program, we can see that the ReplaceFirst method works correctly at least in some situations. Additionally, we can see that it is faster than the regular Replace method in the test case. This is partly because it can stop searching for the value to Replace after a replacement occurred. Implementation differences may better explain the performance change.

Summary

Question and answer

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.

Replace Optimization Replace Performance Algorithms
.NET