- Home /
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 :)
Answer by terdos · Jun 16, 2011 at 05:01 PM
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")
You will very likely have to check for each word you are looking for separately. A simple loop through an array of keywords will work fine, or if you only have a few you can use || in one big if statement.
Another option would be to use regular expressions, I however do not know what support for that exists in mono.
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...
$$anonymous$$IsExpert --> 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?
Answer by Bunny83 · Jun 16, 2011 at 06:24 PM
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
Well, It's code. It would be a pain if it would censor parts of it based on some restricted words.
$$anonymous$$ethod names like WashItClean()
would become Was**tClean
, that's not really helpful ;)
DebrisHitPoint --> Debris**tPoint
BackupIssue --> Backup**sue
Or you talk about the program$$anonymous$$g language Brainfuck :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612110437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you check if a string has already been removed from a generic list? 1 Answer
string deleting 1 Answer
string searching question 1 Answer
Check for a Single word in a String (JS) 1 Answer
Searching XML document 2 Answers