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:

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

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");
}