Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# Replace Extension

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

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(index + patternLength,
	    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");
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < m; i++)
	    string a = value.Replace("$1", "test");
	Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
	    m).ToString("0.00 ns"));
	Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
	    m).ToString("0.00 ns"));


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.