ARandomCoder2K19 ARandomCoder2K19 - 1 year ago 63
C# Question

Remove specific entire word from a string

I have this text file that contains these 3 lines:


How could I remove 'Mike' without removing 'Mike' from 'MikeBob'?

I have tried this:

string text = File.ReadAllText("C:/data.txt");
text = text.Replace("Mike", "");

But it removed all occurrences of Mike.
What should I do?

Answer Source
var text = Regex.Replace(File.ReadAllText("C:/data.txt"), "\bMike\b","");

Pretty easy through regex.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download