Consider the following example.
string s = "The man is old. Them is not bad.";
If I use
s = s.Replace("The", "@@");
Then it returns "@@ man is old. @@m is not bad."
But I want the output to be "@@ man is old. Them is not bad."
How can I do this?
Here's how you'd use a regex, which would handle any word boundaries:
C# console Application
s = s.Replace("The ","@@ ");
I made a comment above asking why the title was changed to assume Regex was to be used.
I personally try to not use Regex because it's slow. Regex is great for complex string patterns, but if string replacements are simple and you need some performance out of it, I'll try and find a way without using Regex.
Threw together a test. Running a million replacments with Regex and string methods.
Regex took 26.5 seconds to complete, string methods took 8 seconds to complete.