Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 951213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:41:53+00:00 2026-05-15T23:41:53+00:00

I am trying to create a bad word filter method that I can call

  • 0

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]”.

I have an SQL table with has a list of bad words, I want to bring them back and add them to a List or string array and check through the string of text that has been passed in and if any bad words are found replace them and return a filtered string back.

I am using C# for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:41:53+00:00Added an answer on May 15, 2026 at 11:41 pm

    Please see this “clbuttic” (or for your case cl[Censored]ic) article before doing a string replace without considering word boundaries:

    http://www.codinghorror.com/blog/2008/10/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea.html

    Update

    Obviously not foolproof (see article above – this approach is so easy to get around or produce false positives…) or optimized (the regular expressions should be cached and compiled), but the following will filter out whole words (no “clbuttics”) and simple plurals of words:

    const string CensoredText = "[Censored]";
    const string PatternTemplate = @"\b({0})(s?)\b";
    const RegexOptions Options = RegexOptions.IgnoreCase;
    
    string[] badWords = new[] { "cranberrying", "chuffing", "ass" };
    
    IEnumerable<Regex> badWordMatchers = badWords.
        Select(x => new Regex(string.Format(PatternTemplate, x), Options));
    
    string input = "I've had no cranberrying sleep for chuffing chuffings days -
        the next door neighbour is playing classical music at full tilt!";
    
    string output = badWordMatchers.
       Aggregate(input, (current, matcher) => matcher.Replace(current, CensoredText));
    
    Console.WriteLine(output);
    

    Gives the output:

    I’ve had no [Censored] sleep for [Censored] [Censored] days – the next door neighbour is playing classical music at full tilt!

    Note that “classical” does not become “cl[Censored]ical”, as whole words are matched with the regular expression.

    Update 2

    And to demonstrate a flavour of how this (and in general basic string\pattern matching techniques) can be easily subverted, see the following string:

    “I’ve had no cranberryıng sleep for chuffıng chuffıngs days – the next door neighbour is playing classical music at full tilt!”

    I have replaced the “i”‘s with Turkish lower case undottted “ı”‘s. Still looks pretty offensive!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.