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

时间:2023-02-10 01:33:22

I am new to c#. I would like to know if a string such as a user name contains a specific word. I want to get those specific words from an array.Here's a example.`

我是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)

(Update)

Thank you all but me learning c# only for about a month could not cover lambda or regex yet. I will have a look at these codes while later.

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

4 个解决方案

#1


2  

You probably want case insensitive validation:

您可能需要不区分大小写的验证:

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

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

Or if you verify on current culture

或者,如果您验证当前的文化

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

Paranoic case involves using regular expressions like this:

Paranoic案例涉及使用这样的正则表达式:

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

   // Nasty user wants to be rude but pass typical filters...
   String name = "A- Bad..WORD..1 !!!";

   String line = Regex.Replace(name, @"\W", "");

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

#2


4  

Use following lambda expression to find if name contains the bad words.

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

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

#3


3  

Here is what I could to do;

这是我能做的;

        // 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));
        }

I also tested other answers too;

我也测试了其他答案;

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));

Here is my simple (&stupid) test code:

这是我的简单(和愚蠢)测试代码:

        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();

Ofcourse, using stopWatch is not accurate. But it's giving an idea.

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

#4


0  

Considering you said you are a beginner. Here is a easier way. I know those answers are better than this but this should be good for beginners.

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

            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();

#1


2  

You probably want case insensitive validation:

您可能需要不区分大小写的验证:

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

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

Or if you verify on current culture

或者,如果您验证当前的文化

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

Paranoic case involves using regular expressions like this:

Paranoic案例涉及使用这样的正则表达式:

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

   // Nasty user wants to be rude but pass typical filters...
   String name = "A- Bad..WORD..1 !!!";

   String line = Regex.Replace(name, @"\W", "");

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

#2


4  

Use following lambda expression to find if name contains the bad words.

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

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

#3


3  

Here is what I could to do;

这是我能做的;

        // 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));
        }

I also tested other answers too;

我也测试了其他答案;

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));

Here is my simple (&stupid) test code:

这是我的简单(和愚蠢)测试代码:

        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();

Ofcourse, using stopWatch is not accurate. But it's giving an idea.

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

#4


0  

Considering you said you are a beginner. Here is a easier way. I know those answers are better than this but this should be good for beginners.

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

            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();