Skip to content

Instantly share code, notes, and snippets.

@woodss
Created December 4, 2014 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodss/408593711511cf546b8f to your computer and use it in GitHub Desktop.
Save woodss/408593711511cf546b8f to your computer and use it in GitHub Desktop.
PottyMouth.cs - Very simple C# Swear Word Filter Extension Method
public static class FormattableObject
{
/// Takes a string and applies a "cleanup" filter on it.
/// Returns a Family friendly string of text with any bad words replaced with
/// the same number of characters, only as asterisks.
/// Usage: myString.ToFamilyFriendlyString()
private static List FilteredWords()
{
List Filter = new List();
Filter.Add("sponge");
Filter.Add("OSX");
Filter.Add("Android");
Filter.Add("person of questionable intelligence");
// Add more here!
return Filter;
}
public static string ToFamilyFriendlyString(this object input)
{
foreach (string fWord in FilteredWords())
{
// Replace the word with *'s (but keep it the same length)
string strReplace = "";
for(int i = 0;i<=fWord.Length;i++) {
strReplace += "*";
}
input = Regex.Replace(input.ToString(), fWord, strReplace, RegexOptions.IgnoreCase);
}
return input.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment