Idan Shechter Idan Shechter - 14 days ago 4x
ASP.NET (C#) Question

Get matched Blacklist words that appears in a provided string

I have an array containing a list of blacklist words as strings. This function returns true if at least one word from the blacklist appears in the text.

private static bool IsSuspectedAsSpam(string text, IEnumerable<string> wordBlacklist)
string temp = text.ToLower();
return wordBlacklist.Any(s => temp.Contains(s));

How can I get the words from the black list that where found in the text, as I want to print them on the web page.


Just use Where instead of Any:

private static IEnumerable<string> GetSpamWords(string text, IEnumerable<string> wordBlacklist)
    return wordBlacklist.Where(s => text.ToLower().Contains(s)).ToList();

This will return the list of strings which were matched in your text.