Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to replace bad words in C#

Discussion in 'Scripting' started by chrismcrae5712, Nov 2, 2014.

  1. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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?
     
  2. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    Well nevermind I literally tried this : itemNameText = Regex.Replace(itemNameText, "F***", ""); and it works perfectly lol Sorry for posting this thread. I guess I know more about C# than I thought lol.
     
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    http://www.scrabblefinder.com/starts-with/f/#words4

    That's a lot of words people can't say though, including Fish and I guess any mention of F***book would be impossible!
     
  4. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    A phonetic algorithm might be needed for spelling variations of essentially the same word.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Works for me!

    --Eric
     
    ZO5KmUG6R and hippocoder like this.
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Basic example. Outputs "f***sticks"
    Code (CSharp):
    1. string swearWord = "fish" ;
    2. string censoredWord = "f***";
    3. string stringWithSwear="fishsticks";
    4.  
    5. void Start(){
    6.  
    7. Debug.Log("Checking if " + stringWithSwear + " contains a swear word");
    8. if(stringWithSwear.Contains(swearWord)){
    9. stringWithSwear = stringWithSwear.Replace(swearWord,censoredWord);
    10. }
    11.  
    12. Debug.Log("Checked string : " + stringWithSwear);
    13. }
    14.  
    15.  
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The problem with that technique is that you end up with the Scunthorpe effect. (Naughty words embedded in legit words.)

    --Eric
     
    gruddlebug likes this.
  8. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  9. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  10. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  11. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
  12. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  13. bobbylee

    bobbylee

    Joined:
    Aug 15, 2013
    Posts:
    49
    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.

    Code (CSharp):
    1. using System.Text;
    2. using System.Collections.Generic;
    3.  
    4.  
    5. private ArrayList wordsToRemove = new ArrayList(new string[]{"fish","123456789101112","123","1234000"});
    6.  
    7.  
    8. void checkwords()
    9.     {
    10.         foreach (string part in wordsToRemove)
    11.         {
    12.     if(String.Equals(part.ToUpper(),SetTextbox.Text.ToUpper()))
    13.             {
    14.          
    15.                 Debug.Log("Name not acceptable, please choose another.");
    16.                 checkword=true;
    17.                 return;
    18.             }
    19.          
    20.         }
    21.      
    22.      
    23.         checkword=false;
    24.      
    25.      
    26. }
    27.  
     
  14. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    That makes no sense when you can just use ToLower().

    --Eric
     
  16. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    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
     
  18. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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?
     
  19. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Still though with this way you will end up with the Scunthorpeeffect as Eric5h5 has explained above.

    Code (CSharp):
    1. if(username.ToLower().Contains(swearWord.ToLower()){
    2. //Find and replace the swear with proper case using some char methods
    3. }
     
  20. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    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.
     
  21. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Basically we are saying it is much easier teo compare it all in one case rather than compare every combination of case manually.
     
  22. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    Ah okay nevermind Now I see what you guys are talking about. Lol blonde moment sorry. Thanks for the posts! I'll give this a shot
     
  23. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    Okay so this method works. Awesome thanks Eric5h5 and aaro4130. You guys were lot's of help!

    How I used it.

    itemNameText = GUI.Textfield.

    if(itemNameText.ToLower().Contains("fish".ToLower()))
    {
    itemNameText = "****";
    }

    works great. :D
     
  24. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You don't need "fish".ToLower() since it's already lowercase. Just "fish" is fine.

    --Eric
     
  25. chrismcrae5712

    chrismcrae5712

    Joined:
    Feb 22, 2014
    Posts:
    25
    Awesome Thanks man!
     
  26. brycebangerter

    brycebangerter

    Joined:
    Oct 17, 2014
    Posts:
    3
    If you are using Regex.Replace, why aren't you also using RegexOptions.IgnoreCase?
     
    ZO5KmUG6R likes this.