How to replace bad words in C#

I am using Regex.Replace to replace anything other than Alphabetical letters or spaces. Is there a way to use Regex to also replace swear words? I am trying to add a parental filter to certain curse words and I am having trouble with doing so. Any pointers?

Well nevermind I literally tried this : itemNameText = Regex.Replace(itemNameText, “fuck”, “”); and it works perfectly lol Sorry for posting this thread. I guess I know more about C# than I thought lol.

That’s a lot of words people can’t say though, including Fish and I guess any mention of F***book would be impossible!

A phonetic algorithm might be needed for spelling variations of essentially the same word.

Works for me!

–Eric

2 Likes

Basic example. Outputs “f***sticks”

string swearWord = "fish" ;
string censoredWord = "f***";
string stringWithSwear="fishsticks";

void Start(){

Debug.Log("Checking if " + stringWithSwear + " contains a swear word");
if(stringWithSwear.Contains(swearWord)){
stringWithSwear = stringWithSwear.Replace(swearWord,censoredWord);
}

Debug.Log("Checked string : " + stringWithSwear);
}

The problem with that technique is that you end up with the Scunthorpe effect. (Naughty words embedded in legit words.)

–Eric

1 Like

well I was actually trying to use the word f u c k but the forums bleeped it out lol but it’s really annoying because any variations of the word f u c k then it’ll work, including a capitalized version of f u c k - such as F u c k and it’ll work.

aaro4130, your method is similar to mine just more laid out and if there is any variation in the swear word, ex Fish, then it wont censor the swear word. I’m not sure how to replace something based on the contained letters and not the exact spelling.

I guess a better question that is more understandable, would be:

How do I replace swear words and their variations?
Ex: itemNameText = Regex.Replace(“fish”, “")
itemNameText = Regex.Replace(“Fish”, "
”)
itemNameText = Regex.Replace(“fIsh”, “")
itemNameText = Regex.Replace(“fiSh”, "
”)
itemNameText = Regex.Replace(“fisH”, “****”)

Except all in one line of code or a more simplified lines of code?

GOAL: to replace the word “fish” and ANY variation of the word “fish”, such as capitalized letters, with ****? Thank you for your time.

Use the strings ToLower method.

Right but if I did it that way too it would only censor the words FISH or fish, not Fish or fIsh or fiSh and so on. I want the combination of the letters fish in any sort, to be censored. I tried “f”+“i”+“s”+“h”, “****” but it does the same thing and only censors the word fish.

Maybe something like this?

I use something like this and it works for me for checking user names. Getting a list of bad words is easy to find. I have about 300 in my list and have no long delay for checking.

using System.Text;
using System.Collections.Generic;
 
 
private ArrayList wordsToRemove = new ArrayList(new string[]{"fish","123456789101112","123","1234000"});
 
 
void checkwords()
    {
        foreach (string part in wordsToRemove)
        {
    if(String.Equals(part.ToUpper(),SetTextbox.Text.ToUpper()))
            {
         
                Debug.Log("Name not acceptable, please choose another.");
                checkword=true;
                return;
            }
          
        }
     
     
        checkword=false;
     
     
}

Yea I am currently using a method similar to this, except I am using an array from another script that has declared an enum list full of all the words I want to ban from my game. However, this list is going to VERY VERY long as I am typing each word over and over and over in all the different ways you can spell them to get around the word ban. Ex, Fish, fish, fIsh, fiSh, fisH and so on and on and on lol This seems to be the easiest, but slowest method so far, thanks for all the replies though guys this is very helpful.

That makes no sense when you can just use ToLower().

–Eric

It does make sense because when I use ToLower() all that does is changes all the letters to lower case. Which means I have to make another line of code for ToUpper(). Which is exactly what I am doing right now just without using ToLower or ToUpper.

No, that’s not how it works at all. You use ToLower to compare a word such as “Fish” to the target word, namely “fish”. Since ToLower changes everything to lowercase, that means “FisH”, “fISH”, “Fish”, etc. all match “fish”. You wouldn’t actually change the string itself, just what you’re comparing.

–Eric

What do you mean compare? The Only way I know how to use ToLower is something like itemNameText = “fish”.ToLower(); and all that does is changes the word to lower case?

Still though with this way you will end up with the Scunthorpeeffect as Eric5h5 has explained above.

if(username.ToLower().Contains(swearWord.ToLower()){
//Find and replace the swear with proper case using some char methods
}

Yea I don’t know what Eric means with the compare, if there is a different way to use ToLower or ToUpper then please do explain, other wise I have no idea where you’re going with this? I know there are ways to replace swear words with other texts, I’ve seen lots of demonstrations, just never anyone explaining how to do it.