c# - 检查字符串是否包含列表中的所有输入

标签 c#

我希望能够检查字符串是否包含列表中的所有值; 因此,如果您的答案列表中包含所有“关键词”,它只会给您一个“正确答案”。 这是我厌倦的事情,其中​​一半失败了;(不检查所有数组,只接受一个)。 代码我累了:

 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中的关键词:study,mental process,behaviour,humans

Answer: Psychology is the study of mental process and behaviour of humans

现在,当且仅当上面的答案包含所有关键词时,我的代码才会接受答案。 我希望我已经清楚这一点。

编辑:感谢大家的帮助。所有答案都已投票通过,我感谢大家的快速回答。我投票赞成可以轻松适应任何代码的答案。 :)

最佳答案

使用 LINQ:

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

关于c# - 检查字符串是否包含列表中的所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728294/

相关文章:

c# - 有没有办法让字符串达到年份值?

c# - 从经典 ASP 调用 c# webservice

c# - 支持来自不同供应商的多个工业相机?

c# - 如何检查控件是否启用了触摸手势?

c# - 无法在 Visual Studio 2010 中加载文件或程序集

c# - xml :space in resource file of c# winform

c# - 将 bool 值绑定(bind)到 visualstate

c# - 在类 C# 中使用 ref

c# - MVC3 : Overriding the Model values in HTTPPost action method

c# - 调试时是否可以将 "see"放入 C# foreach 循环的迭代集合中?