C# - Fastest way to find one of a set of strings in another string
.net, c#, regex, string
Solution
You could try a regex, but I'm not sure it's faster.
Regex rx = new Regex("(" + string.Join("|", swearWords) + ")");
rx.IsMatch(myString)
Problem
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.