Check if the row contains all the entries in the list

I want to check if a string contains all the values ​​stored in a list; Therefore, it will only give you the “correct answer” if you have all the “keywords” from the list in your answer. Heres something I'm tired that half fails (doesn't check all arrays, only one will accept). The code is tired:

foreach (String s in KeyWords) { if (textBox1.Text.Contains(s)) { correct += 1; MessageBox.Show("Correct!"); LoadUp(); } else { incorrect += 1; MessageBox.Show("Incorrect."); LoadUp(); } } 

Essentially, I want:

Question: What is the definition of psychology?

Keywords in arraylist: research, mental process, behavior, people

Answer: Psychology is a study of the mental process and behavior of people.

Now, if ONLY if the answer above contains all the keywords, my code will accept the answer. Hope I figured it out.

Edit: Thank you all for your help. All replies were rejected, and I thank everyone for the prompt replies. I voted for the answer, which can be easily adapted to any code. :)

+8
c #
source share
3 answers

Using LINQ:

 // case insensitive check to eliminate user input case differences var invariantText = textBox1.Text.ToUpperInvariant(); bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant())); 
+13
source share

You can use some of the LINQ methods, for example:

 if(Keywords.All(k => textBox1.Text.Contains(k))) { correct += 1; MessageBox.Show("Correct"); } else { incorrect -= 1; MessageBox.Show("Incorrect"); } 

The All method returns true when the function returns true for all elements in the list.

+2
source share

This should help:

  string text = "Psychology is the study of mental process and behaviour of humans"; bool containsAllKeyWords = KeyWords.All(text.Contains); 
+2
source share

All Articles