public static void Main()
string testString = "Hello, I am Hello, You are Declan";
Console.WriteLine(Replace(testString, "Hello", "Mark", 2));
public string Replace(string text, string search, string replace, int instanceNumber)
for(int i=1; i<instanceNumber;i++)
pos = text.IndexOf(search, pos);
throw new Exception("There were only " + instanceNumber + " instances of the phrase");
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);