How to replace exact matching strings only with replacement 2

I am trying to replace a string, but due to a few matching words, it replaces both strings.

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 goes in two places, so it replaces both IS_CHILD.

I want to replace only the exact word IS_CHILD.

how to do it?

+4
source share
7 answers

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 
+1
source

The easiest way is to use Regex:

 val3 = Regex.Replace(Regex.Replace(str, @"\bIS_PARENT\b", Val1), @"\bIS_CHILD\b", Val2); 
+1
source

A very easy way to do this:

 val3 = str.Replace("IS_PARENT", Val1 ).Replace(", IS_CHILD", ", "+val2); 

but in this case, why don't you just do:

 val3 = val1 + ", IS_CHILD_WITH_ROLE, " + val2; 

where is str coming from?

+1
source

Use LastIndexOf , cut the line manually and paste the replacement in the right place. Like this:

 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 "; ReplaceLast(str, "IS_PARENT", Val1 ); ReplaceLast(str, "IS_CHILD", Val2 ); public static string ReplaceLast(this string value, string oldWord, string newWord) { int index = value.LastIndexOf(oldWord); String modified = value.Remove(index, oldWord.Length); modified = modified.Insert(index, newWord); return modified; } 

This is perhaps the least processor power and memory consuming way to do what you need in this situation. This is also the lowest level.

0
source

You can try using regular expressions in the extension method to maintain the same syntax style:

 public static class Extensions { public static string ReplaceExact(this string value, string oldWord, string newWord) { Regex r = new Regex(string.Format(@"\b{0}\b", oldWord)); return r.Replace(value, newWord); } } 

and use it:

 string str = "IS_CHILD, IS_CHILD ,IS_CHILD, IS_PARENT, IS_CHILD_WITH_ROLE, IS_CHILD"; string result = str.ReplaceExact("IS_CHILD", "000") .ReplaceExact("IS_PARENT", "111"); Console.WriteLine(result); //prints: 000, 000 ,000, 111, IS_CHILD_WITH_ROLE, 000 
0
source

Maybe this will help

 string child = "IS_PARENT, IS_CHILD_WITH_ROLE, IS_CHILD", resultString = string.Empty; List<string> chunks = child.Split(' ').ToList(); chunks.ForEach(delegate(string i) { if (string.Equals(i, "IS_CHILD", StringComparison.CurrentCultureIgnoreCase)) resultString += "ReplaceString "; else resultString += i + " "; }); Console.WriteLine(child); Console.WriteLine(resultString); Console.Read(); 
0
source

Use like this Body.Replace ("\ bat \ b", "at")

0
source

All Articles