Home > Enterprise >  Unable to compare strings while implementing Profanity filter in Unity3d
Unable to compare strings while implementing Profanity filter in Unity3d

Time:12-13

I am implementing a simple profanity filter for my player username in unity. But for some reason, I am not getting the correct result. As shown in code below, I am extracting the words from a TextAsset .txt file into a list of strings and looping through the list to check if the username contains a badword using Contains() function. But the Contains() function is always returning false. What else I tried :

  1. Tried "==" operator to test for exact string, but still returning false
  2. Tried Equals() function to test for exact string, but still returning false
  3. Tried regex but still not working Note : both checkText and badWordToUpper are of type string and are showing right results. But when compared, not working. enter image description here

CodePudding user response:

Sound like a white char is hidden in a bad word. If you are on Windows, the break line is "\r\n". You can check with :

void Start()
{
    strBlockList = ...;
    var wordWithWhiteChar = strBlockList.Where(w => w.Any(c => char.IsWhiteSpace(c))).ToList();
    // Check wordWithWhiteChar
}

If it's the case, you need to clean each word like :

void Start()
{
    strBlockList = ...;
    for (int i = 0; i < strBlockList.Length ; i  )
    {
        strBlockList[i] = Regex.Replace(strBlockList[i], @"\s ", string.Empty).ToUpper();
    }
}

Bonus : To upper case in Start to cache the operation.

  • Related