C# 需要帮助在输入句子中查找匹配词

C# Need Help Finding matching word in input sentence

好的,我遇到了麻烦。我有一个包含三个文本框和一个按钮的表单,在第一个文本框 (textBox1) 中,用户输入了一个句子。在下一个文本框 (textBox3) 中,用户输入一个词。然后用户单击一个按钮,第三个文本框 (textBox2) 检查输入的单词是否与句子中的单词匹配。我不能使用任何类型的 "shortcuts",我只能走很长的路(没有 .Compare 或类似的东西)。这是我目前所拥有的:

 private void button1_Click(object sender, EventArgs e)
  {



        string inSentence = textBox1.Text.ToUpper();

        string txtB2 = textBox3.Text.ToUpper();

        string[] inWord;

        inWord = inSentence.Split(' ');

        int wordCount = inWord.Length;

        for (int i = 0; i < wordCount; i++)
        {
            if (txtB2 == inWord[i])
            {
                textBox2.Text = "Yes";
            }
            else
                textBox2.Text = "No";
        }

  }

我遇到的问题是,假设我在第一个框中键入 "hi its me",唯一匹配的词是 "me"。它只匹配最后一个词。它并不匹配所有内容。

同样,我只能以这种方式进行。我只是想知道我哪里出错了,为什么。如果有人能帮助我,我将不胜感激。

我也试过用这个

  foreach (string n in inWord)
        {
            textBox2.Text = inWord + " ";
            for (int i = 0; i < wordCount; i++)
            {

                if (txtB2 == n)
                {
                    textBox2.Text = "Yes " + n;
                }
                else
                    textBox2.Text = "No " + n;
            }
        }

我遇到了同样的问题(我在文本输出中添加了 "n" 以检查它将出现在哪个词上,当我输入 "hi its me" 时它总是出现在 "ME" ).

不使用正则表达式或 IndexOf,并且假设您必须使用非常低效的方法遍历下面表达的每个单词,您需要在找到匹配项时中断循环的执行。您应该使用 while 构造而不是 for 循环来完成此操作。

bool found = false;
int i = 0;
while (!found && i < wordCount)
{
    if (txtB2 == inWord[i])
    {
        textBox2.Text = "Yes";
        found = true;
    }
    else
        textBox2.Text = "No";

    i++;
}

问题:你的代码中的"problem"总是检查每个单词。找到匹配项后,循环不会停止。

解决方案: 在将文本框设置为 [=22= 后,在 if 语句中添加 return;(或 break)语句]

试试正则表达式

string text = "Your sentence";
string pat = "[^\s]word[$\s]"; //[rowstart or whitespace]word[rowend or whitespace]
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match m = r.Match(text);
if(m.Success) {
   // The sentence contains the word
}

你所做的是每次循环整个句子。您检查每个单词,包括最后一个单词,每个单词的 textBox2.Text 都在变化。你看到的是最后一句话的结果。

    textBox2.Text == "No"; // Initially set the text to "No"
    for (int i = 0; i < wordCount; i++)
    {
        if (txtB2 == inWord[i])
        {
            textBox2.Text = "Yes"; // If a matching word is found change text to yes
            break;
        }
    }

如果你想使用 foreach,你可以像这样省略另一个 for 循环:

textBox2.Text = "No";
foreach(string word in inWord)
{
    if(txtB2 == word)
        textBox2.Text = "Yes";
}

foreach 通过遍历 inWord 并将当前元素的值放入 word

如果唯一的限制是你不使用正则表达式,那么你可以这样做

   var wordArray =  textBox1.Text.ToUpper().Split(" ");
    var response = wordArray.Where(x=> x.Trim().Equals(textbox2Input.ToUpper());
//the base implementation of the equals method on strings is enough

if(response.Any())
        textbox3.Text ="Yes";
    else 
    textbox3.Text="NO"

如果您想要一个实用的方法来解决这个问题,那就是:

var inSentence = textBox1.Text;
var wordToFind = textBox3.Text;

var wordFound = Regex.IsMatch(@"\b" + inSentence + @"\b", wordToFind, RegexOptions.IgnoreCase);

如果这是人为设计的学校作业,那么 1. 您的老师可能应该想出一个更好的练习,并且 2. 您确实需要自己完成作业。