Reza Taibur
Reza Taibur

Reputation: 261

Check if a string contains a specific string using array

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.`

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.

Upvotes: 7

Views: 857

Answers (4)

Reza Taibur
Reza Taibur

Reputation: 261

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

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

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:

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

Upvotes: 2

Lost_In_Library
Lost_In_Library

Reputation: 3483

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.

Upvotes: 3

M.S.
M.S.

Reputation: 4413

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

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

Upvotes: 4

Related Questions