【问题标题】:String [] but [] in the script [duplicate]脚本中的字符串 [] 但 [] [重复]
【发布时间】:2020-07-10 01:04:21
【问题描述】:

我正在为我的游戏制作一个简单的坏词过滤器! badwords = string[] (也就是说我可以把所有应该被屏蔽的坏词一一统一放在strings[]中。(图一)

问题是我希望阻止许多坏词,并且将所有坏词放入字符串中可能需要太多时间 [] 一一。

解决方案:是否有可能以某种方式将所有坏词放入脚本中?这样我就可以从坏词列表中复制和粘贴它们?

我尝试过的:我尝试将 string[] 设置为普通字符串,然后 string = "badwords" 然后出现错误表示字符串不能转成char之类的(图4)

感谢您的帮助! :D

编辑:那是新代码,我不确定如何将它变成 string[] 坏词? 我现在注意到,在第二张图片中,“String badwords”没有[],但通常在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();
}

【问题讨论】:

  • 您仍应使用string[]。您只需要处理坏词列表中的字符串。坏词列表是什么样的?
  • 但是不要发布实际的坏词列表 - 你可能会因为“粗鲁!”而关闭帖子。
  • post code,不是图片。并且不要添加不相关的标签; visual-studio 是针对 Visual Studio 应用程序的问题,而不是告诉我们您正在使用 Visual Studio。同样,unity3d 用于解决 Unity 包的问题。
  • @Sweeper 嘿!感谢您的时间!坏词列表如下所示:bad、bad、bad 等。
  • this 有帮助吗?

标签: c# string filter


【解决方案1】:

您可以使用 string.Split() 并使用带有逗号分隔值的文本文件:

 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: ,
    }
}

}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-03-15
  • 2010-12-12
  • 1970-01-01
  • 2013-09-25
  • 2016-09-03
  • 2021-09-13
  • 2021-09-11
  • 1970-01-01
相关资源
最近更新 更多