简体   繁体   中英

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.

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. Use the debugger or Console.WriteLine().

This is where unit testing becomes valuable. NUnit or MSTest are available for C#/VS development.

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.

KeyWords.Clear();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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