使用数组检查字符串是否包含特定字符串

时间:2016-03-28 12:59:58

标签: c# arrays

我是c#的新手。我想知道一个字符串,如用户名是否包含特定的单词。我想从数组中获取这些特定的单词。这是一个例子。

Console.Write("Name: ");
_name = Console.ReadLine();
name = Program.ProperNameMethod( _name);
Console.WriteLine();

string[] badWordArray = { "aBadword1", "aBadWord2", "aBadWord3" };

if (!string.IsNullOrEmpty(name) // Would like to check for the badwordarray aswell)

(更新)

谢谢大家,但是我学习c#只用了大约一个月就无法覆盖lambda或regex了。我稍后会看看这些代码。

4 个答案:

答案 0 :(得分:4)

使用以下lambda表达式查找name是否包含坏词。

 bool nameHasBadWords = badWordArray.Any(p => name.Contains(p));

答案 1 :(得分:3)

这是我能做的;

        // Lowered bad words array
        string[] badWordArray = { "abadword1", "abadword2", "abadword3" };

        bool isBadWord = false;
        if (!string.IsNullOrEmpty(name))
        {
            name = name.ToLower();
            isBadWord = badWordArray.Any(badWord => name.Contains(badWord));
        }

我也测试了其他答案;

459 ms

.Any(badWord => string.Equals(name, badWord, StringComparison.CurrentCultureIgnoreCase));

1464 ms

.Any(badWord => name.IndexOf(badWord, StringComparison.CurrentCultureIgnoreCase) >= 0);

247 ms

.Any(badWord => name.Contains(badWord));

这是我的简单(& stupid )测试代码:

        var name = "tuckyou";

        // Lowered bad words array
        string[] badWordArray = { "abadword1", "abadword2", "abadword3" };

        Stopwatch stopwatch = new Stopwatch();

        int oneMillion = 1000000;

        bool isBadWord = false;

        stopwatch.Start();
        for (int i = 0; i < oneMillion; i++)
        {
            isBadWord = badWordArray.Any(badWord => string.Equals(name, badWord, StringComparison.CurrentCultureIgnoreCase));
        }
        stopwatch.Stop();

        Console.WriteLine(stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        stopwatch.Start();
        for (int i = 0; i < oneMillion; i++)
        {
            isBadWord = badWordArray.Any(badWord => name.IndexOf(badWord, StringComparison.CurrentCultureIgnoreCase) >= 0);
        }
        stopwatch.Stop();

        Console.WriteLine(stopwatch.ElapsedMilliseconds);
        stopwatch.Reset();

        stopwatch.Start();
        if (!string.IsNullOrEmpty(name))
        {
            name = name.ToLower();
        }

        for (int i = 0; i < oneMillion; i++)
        {
            isBadWord = badWordArray.Any(badWord => name.Contains(badWord));
        }
        stopwatch.Stop();

        Console.WriteLine(stopwatch.ElapsedMilliseconds);

        Console.ReadLine();

当然,使用stopWatch并不准确。但它提出了一个想法。

答案 2 :(得分:2)

您可能希望不区分大小写验证:

propTypes

或者,如果您在当前文化

上进行验证
string[] badWordArray = { "aBadword1", "aBadWord2", "aBadWord3" };

Boolean isBadWord = badWordArray
  .Any(badWord => name.IndexOf(badword, StringComparison.OrdinalIgnoreCase) >= 0);

Paranoic 案例涉及使用正则表达式,如下所示:

Boolean isBadWord = badWordArray
  .Any(badWord => name.IndexOf(badWord, StringComparison.CurrentCultureIgnoreCase) >= 0);

答案 3 :(得分:0)

考虑到你说你是初学者。这是一种更简单的方法。我知道这些答案比这更好,但这对初学者来说应该是好的。

            Console.Write("Name: ");
            string userInput = Console.ReadLine();

            //The words that you dont want your user to have
            string[] array = new string[2];
            array[0] = "bad";
            array[1] = "reallybad";

            for (int i = 0; i < array.Length; i++)
            {

                //Used to lower so user cant get away with stuffs like: rEALLyBAd
                if (userInput.Contains(array[i].ToLower()))
                {

                    Console.WriteLine("Invalid name!");

                }

            }

            Console.ReadKey();