Check if the string contains all inputs on the list

后端 未结 3 1612
囚心锁ツ
囚心锁ツ 2021-01-04 12:11

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 l

相关标签:
3条回答
  • 2021-01-04 12:27

    You can use some of the LINQ methods like:

    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 of the items in the list.

    0 讨论(0)
  • 2021-01-04 12:31

    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()));
    
    0 讨论(0)
  • 2021-01-04 12:41

    This should help:

     string text = "Psychology is the study of mental process and behaviour of humans";
     bool containsAllKeyWords = KeyWords.All(text.Contains);
    
    0 讨论(0)
提交回复
热议问题