ARandomCoder2K19 ARandomCoder2K19 - 9 months ago 43
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.