ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Replace

Case-insensitive replace method

Source

public static String Replace(this String originalString, String oldValue, String newValue, StringComparison comparisonType)
{
	Int32 startIndex = 0;

	while (true)
	{
		startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType);
				
		if (startIndex < 0)
		{
			break;
		}

		originalString = String.Concat(originalString.Substring(0, startIndex), newValue, originalString.Substring(startIndex + oldValue.Length));

		startIndex += newValue.Length;
	}

	return (originalString);
}

Example

String newString = "AbC".Replace("b", "B", StringComparison.OrdinalIgnoreCase);

Author: Ricardo Peres

Submitted on: 1 feb 2012

Language: C#

Type: System.String

Views: 6552