You need two calls to Substring() , not one: one to get textBefore , and the other to textAfter , and then you combine those who have your replacement.
int start = s.IndexOf("{"); int end = s.IndexOf("}"); //I skip the check that end is valid too avoid clutter string textBefore = s.Substring(0, start); string textAfter = s.Substring(end+1); string replacedText = textBefore + newText + textAfter;
If you want to keep curly braces, you need a little tweaking:
int start = s.IndexOf("{"); int end = s.IndexOf("}"); string textBefore = s.Substring(0, start-1); string textAfter = s.Substring(end); string replacedText = textBefore + newText + textAfter;
source share