Replace first occurrence of pattern in a string [duplicate]

I think you can use the overload of Regex.Replace to specify the maximum number of times to replace…

var regex = new Regex(Regex.Escape("o"));
var newText = regex.Replace("Hello World", "Foo", 1);

Leave a Comment