I am trying to create a bad word filter method that I can call before every insert and update to check the string for any bad words and replace with \"[Censored]\".
Although I'm a big fan of Regex, I think it won't help you here. You should fetch your bad word into a string List or string Array and use System.String.Replace
on your incoming message.
Maybe better, use System.String.Split
and .Join
methods:
string mayContainBadWords = "... bla bla ...";
string[] badWords = new string[]{"bad", "worse", "worst"};
string[] temp = string.Split(badWords, StringSplitOptions.RemoveEmptyEntries);
string cleanString = string.Join("[Censored]", temp);
In the sample, mayContainBadWords
is the string you want to check; badWords
is a string array, you load from your bad word sql table and cleanString
is your result.