7

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.

3
  • what is ProperNameMethod Commented Mar 28, 2016 at 13:03
  • @Reza Taibur please mark my answer as accepted if it helps you. Commented Mar 28, 2016 at 13:06
  • I made a method that would convert the first word of the string to a Block letter.@un-lucky Commented Mar 28, 2016 at 13:52

4 Answers 4

4

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

 bool nameHasBadWords = badWordArray.Any(p => name.Contains(p));
Sign up to request clarification or add additional context in comments.

2 Comments

what is that "p"? @Mahesh Chand?
p is an alias used to represent individual element of the array.
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.

Comments

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:

   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 Comments

I liked your "paranoic case" answer sir. But using contains is more efficient then to use regex.
@Lost_In_Library: it depends on what you want to achieve. In case of efficiency (huge array of bad words) and lax control you're quite right. But if you want to eliminate just a possibility of being rude, a shadow of a bad word, you have to use regular expressions and the like.
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.