简体   繁体   English

检查列表是否包含字符串中的所有单词

[英]Check if list contains all words in string

Well i was reading Check if the string contains all inputs on the list And tried it, but when i had something like a basic question such as 'Who discovered Austrlia' and if i put the key words in the answer as 'captain,cook' it would say i got the question wrong. 好吧,我正在阅读检查字符串是否包含列表中的所有输入并进行了尝试,但是当我遇到诸如“谁发现了Austrlia”之类的基本问题时,如果我在答案中输入了“ captain,cook”作为关键词,它会说我错了。

Any idea what im doing wrong; 不知道我在做什么错; code im using: 使用以下代码:

            GivenAnswer = textBox1.Text;
            String invariantText = textBox1.Text.ToUpperInvariant();
            bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant()));
                if (matches)
                {
                    correct++;
                    if (InstantResult) { MessageBox.Show("Questions Answered Correctly", "Answer Results", MessageBoxButtons.OK, MessageBoxIcon.Information); }
                }
                else
                {
                    incorrect++;
                    if (InstantResult) { MessageBox.Show("Question Answered Wrong, sorry!", "Answer Result", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
                }
            Study_Helper.Form1.QuestionResults.Add(Question + "|" + (matches ? "true" : "false") + "|" + (Exact? "N/A" : KeyWords_Found()) + "|" + (Hint_Used ? "true" : "false") + "|" + GivenAnswer.ToLowerInvariant());
            LoadUp();
            textBox1.Clear();
            textBox1.Focus();

For debugging purposes, confirm that KeyWords and invariantText contain expected values. 出于调试目的,请确认KeyWordsinvariantText包含期望值。 Use the debugger or Console.WriteLine(). 使用调试器或Console.WriteLine().

This is where unit testing becomes valuable. 这是单元测试变得有价值的地方。 NUnit or MSTest are available for C#/VS development. NUnit或MSTest可用于C#/ VS开发。

Thanks to the first comment on my question. 感谢对我的问题的第一条评论。 I relised i never actually cleared the arraylist, make sure you clear the arraylist after you do this check as to not get more values in your keyword list then wanted. 我亲戚说,我从未真正清除过arraylist,请确保在执行此检查后清除arraylist,以免在关键字列表中得到想要的更多值。

KeyWords.Clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM