c# – How to cut specified words from string

c# – How to cut specified words from string

Simple approaches to profanity filtering wont work – complex approaches dont work, for the most part, either.

What happens when you get a work like password and you want to filter out ass? What happens when some clever person writes a$$ instead – the intent is still clear, right?

See How do you implement a good profanity filter? for extensive discussion.

You could use RegEx to make things a little cleaner:

var bannedWords = @b(this|is|the|list|of|banned|words)b;

foreach(mail in mailList)
    var clean = Regex.Replace(mail, bannedWords, , RegexOptions.IgnoreCase);

Even that, though, is far from perfect since people will always figure out a way around any type of filter.

c# – How to cut specified words from string

Youll get best performance by drawing up a finite state machine (FSM) (or generate one) and then parsing your input 1 character at a time and walking through the states.

You can do this pretty easily with a function that takes your next input char and your current state and that returns the next state, you also create output as you walk through the mail messages characters. You draw the FSM on a paper.

Alternatively you could look into the Windows Workflow Foundation: State Machine Workflows.

In that way you only need to walk each message a single time.

Leave a Reply

Your email address will not be published. Required fields are marked *