Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Fastest way to find one of a set of strings in another string

I need to check whether a string contains any swear words.

Following some advice from another question here, I made a HashSet containing the words:

HashSet<string> swearWords = new HashSet<string>() { "word_one", "word_two", "etc" };

Now I need to see if any of the values contained in swearWords are in my string.

I've seen it done the other way round, eg:

swearWords.Contains(myString)

But this will return false.

What's the fastest way to check if any of the words in the HashSet are in myString?

NB: I figure I can use a foreach loop to check each word in turn, and break if a match is found, I'm just wondering if there's a faster way.

like image 840
shauneba Avatar asked Apr 11 '12 09:04

shauneba


2 Answers

If you place your swears in an IEnumerable<> implementing container:

var containsSwears = swarWords.Any(w => myString.Contains(w));

Note: HashSet<> implements IEnumerable<>

like image 109
Sprague Avatar answered Oct 15 '22 15:10

Sprague


You could try a regex, but I'm not sure it's faster.

Regex rx = new Regex("(" + string.Join("|", swearWords) + ")");
rx.IsMatch(myString)
like image 36
McGarnagle Avatar answered Oct 15 '22 13:10

McGarnagle