You can use Regex.Replace
string newString = Regex.Replace(str, @"\bIS_CHILD\b", replacedText);
EDIT: If you want to replace IS_PARENT with the same criteria as IS_CHILD (instead of the whole word)
string newString = Regex.Replace(newString, @"\bIS_PARENT\b", "NEW TEXT"); newString = Regex.Replace(str, @"\bIS_CHILD\b", replacedText);
OR (this is not optimized in the answer either, because this is one way to replace one word)
You can split the string based on space, and then use string.Join to create a new line after replacing the full word.
string str = "IS_PARENT, IS_CHILD_WITH_ROLE, IS_CHILD"; string replacedText = "SomeThing"; string newString = string.Join(" ", str.Split() .Select(r => r == "IS_CHILD" ? replacedText : r));
The new line will be:
IS_PARENT, IS_CHILD_WITH_ROLE, SomeThing
source share