ARandomCoder2K19 ARandomCoder2K19 - 28 days ago 6
C# Question

Remove specific entire word from a string

I have this text file that contains these 3 lines:

Bob
MikeBob
Mike


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
var text = Regex.Replace(File.ReadAllText("C:/data.txt"), "\bMike\b","");

Pretty easy through regex.

Comments