检查字符串是否包含列表中的所有输入 [英] Check if the string contains all inputs on the list
问题描述
我希望能够检查字符串是否包含列表中保存的所有值;因此,仅当您的答案列表中包含所有关键词"时,它才会为您提供正确答案".这是我很累的东西,有一半失败了((不检查所有数组,只接受一个).代码我很累:
I want to be able to check if the string contains all the values held in the list; So it will only give you a 'correct answer' if you have all the 'key words' from the list in your answer. Heres something i tired which half fails;(Doesn't check for all the arrays, will accept just one). Code i tired:
foreach (String s in KeyWords)
{
if (textBox1.Text.Contains(s))
{
correct += 1;
MessageBox.Show("Correct!");
LoadUp();
}
else
{
incorrect += 1;
MessageBox.Show("Incorrect.");
LoadUp();
}
}
基本上我想做的是:
问题:心理学的定义是什么?
Question: What is the definition of Psychology?
arraylist中的关键字:研究,心理过程,行为,人类
Key words in arraylist: study,mental process,behaviour,humans
答案:心理学是心理过程的研究,是人类
现在并且仅当以上答案包含所有关键字时,我的代码才会接受答案.我希望我对此很清楚.
Now if and ONLY if the answer above contains all key words will my code accept the answer. I hope i have been clear with this.
编辑:谢谢大家的帮助.所有答案均已投票表决,我感谢大家的快速答复.我投票赞成可以轻松地适用于任何代码的答案.:)
Thank you all for your help. All answers have been voted up and i thank everyone for quick answers. I voted up the answer that can be easily adapted to any code. :)
推荐答案
使用LINQ:
// case insensitive check to eliminate user input case differences
var invariantText = textBox1.Text.ToUpperInvariant();
bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant()));
这篇关于检查字符串是否包含列表中的所有输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!