I am trying to replace string but due to few matching words it is replacing both the string.
Val1 = "CASE WHEN [" + isParent + "] = 0 THEN 'False' WHEN [" + isParent + "] = 1 THEN 'True' END ";
Val2 = "CASE WHEN [" + isChild + "] = 0 THEN 'False' WHEN [" + isChild + "] = 1 THEN 'True' END ";
val3 = str.Replace("IS_PARENT", Val1 ).Replace("IS_CHILD", val2);
in my
str = "IS_PARENT, IS_CHILD_WITH_ROLE, IS_CHILD";
IS_CHILD is coming in two place so it is replacing both IS_CHILD.
I want to replace only exact word IS_CHILD.
how to do that?
You can use Regex.Replace
EDIT:
If you want to replace IS_PARENT with the same criteria as IS_CHILD (replacing the whole word only)
OR(This is not optimized and in the answer because it is one way to replace a single word)You can split the string based on the space, and then use string.Join to create a new string after replacing the complete word.
New String will be: