0

I am making a simple badword filter for my game! The badwords = string[] (that means that i can put all the bad words, that should be blocked, in strings[] in unity one by one.(picture one)

The problem is that i want many badwords to be blocked and it would probably take too much time to put all the badwords in strings[] one by one.

Solution: Is it possible to somehow put all the bad words in a script? So that i for example can copy&paste them from a badword list?

What I've tried: I've tried to make the string[] to a normal string and then string = "badwords" then i got an error that said that the string can't be converted to an char or something like that (picture 4)

I am thankful for any help! :D

This is where i put the bad words one by one script The script that removes the bad words from the text

Edit: That is the new code, i'm not really sure how to make this to the string[] badwors? I now noticed that in the second picture, there is no [] for the "String badwords", but usually there is a [] after the string(String[] badwords;)

public static void FormatBadWords()
{
    string pattern = "[,]+";
    string input = "bad, toobad, crazy";
    string[] result = Regex.Split(input, pattern,
                                  RegexOptions.IgnoreCase,
                                  TimeSpan.FromMilliseconds(500));
    for (int ctr = 0; ctr < result.Length; ctr++)
    {
        Console.Write("'{0}'", result[ctr]);
        if (ctr < result.Length - 1)
            Console.Write(", ");
    }
    Console.WriteLine();
}
  • 1
    You should still use a `string[]`. You just need to process the string from the bad words list. What does the bad words list look like? – Sweeper Mar 29 '20 at 16:35
  • But *please* don't post the actual list of bad words - you'll probably then get the post closed as "rude!" – Adrian Mole Mar 29 '20 at 16:43
  • Please [post code](https://idownvotedbecau.se/imageofcode), not images. And do not add irrelevant tags; `visual-studio` is for problems with the Visual Studio application, not to tell us that you are using Visual Studio. Similarly `unity3d` is for problems with the Unity package. – Dour High Arch Mar 29 '20 at 16:53
  • @Sweeper Hey! Thank you for your time! The bad words list looks like that: bad, bad, bad, etc. –  Mar 29 '20 at 17:18
  • Does [this](https://stackoverflow.com/questions/1126915/how-do-i-split-a-string-by-a-multi-character-delimiter-in-c) help? – Sweeper Mar 29 '20 at 17:21
  • @Sweeper Thank you! The only problem now is how to get the code to be "string[] badwords" The code: Oh, i don't know how to post the code here but i edited my main question! :D –  Mar 29 '20 at 19:15

1 Answers1

0

You can use string.Split() and use a text file with comma-separated values:

 class Program
{
    static void Main(string[] args)
    {
        //Assume this as your string that needs fiter
        string myString = "These are my bad words: bad, toBad";

        //Put words in a file or simply hardcode: string badWordsCommaSeperated = "bad, toBad"
        string badWordsCommaSeperated = File.ReadAllText(@"..\..\BadWords.txt");

        //Catch the above Comma-Seperated string in string array
        string[] badWords = badWordsCommaSeperated.Split(',');

        // Loop all bad words in the array using foreach and replace myString
        foreach(string badWord in badWords)
        {
            myString = myString.Replace(badWord, "");
        }

        Console.WriteLine(myString);
        //Final Value of myString: These are my  words: ,
    }
}

}