Search A String For Keyword - Profanity

Hello,

I have a GUI TextField in my game where you can submit a string of text to my server.

What I want to do though, is to search the string for any profanity and offensive words before submitting.

if(GUI.Button(Rect(100,120,150,30),"Create")){
   if(newPlayer == "f**k" || newPlayer == "s**t"){
      print("Profanity Found");
   }
   else{
      AddVariables();
   }
}

The problem with the methord I’m using is that it will only pull the string if it says “f–k” - where as some genius might submit “KingOfF–King”. So is there a way to search the whole string for specific keywords and pull it up?

Thanks :slight_smile:

Yes. I consider this bad only because every newbie programmer will put it in some kind of Update sooner or later. Just wait.

2 Answers

2

newPlayer.IndexOf("f**k") will return the position in the string that f**k occurs, or -1 if it does not exist.

Cool - is there a way to search multiple keywords? Or will I just have to do an OR: newPlayer.IndexOf("f--k") || newPlayer.IndexOf("s--t")

Okay, one more thing - will this work for strings like "iamoneuglyf--ker" - so its all one word.

It doesn't matter. IndexOf just looks for character sequences. There is nothing like a "word" :D. It's one string. Be careful with your keywords. In some names you will get false alerts... TomIsExpert --> tomiSEXpert

Okay - I have tried this methord (with || or || statements) and if I, for example type in 'hello', I get a 'profanity found' - why do you think that is?

A short example:

var keywords : String[] = (new Array("fuck", "shit")).ToBuiltin(String);

function CheckForProfanity(text : String) : boolean
{
    text = text.ToLower(); // make the string lower case so we can search for the keywords case-insensitive.
	for(var keyword:String in keywords)
	{
	    if (text.IndexOf(keyword)>=0)
		    return true;
	}
	return false;
}

To check a string do:

var newPlayerName = "TestName";

if (CheckForProfanity(newPlayerName))
{
    Debug.Log("illegal word detected");
}

interesting that unity answers does not have auto-censor enabled

now why would it do that?

Well, It's code. It would be a pain if it would censor parts of it based on some restricted words. Method names like WashItClean() would become Was**tClean, that's not really helpful ;) DebrisHitPoint --> DebristPoint BackupIssue --> Backupsue Or you talk about the programming language <a href="http://en.wikipedia.org/wiki/Brainfuck">Brainfuck</a> :D