c#需要帮助查找输入句子中的匹配词

本文关键字:句子 输入 帮助 查找 | 更新日期: 2023-09-27 18:03:32

好吧,我遇到麻烦了。我有一个表单,它有三个文本框和一个按钮,在第一个文本框(textBox1)中,用户输入一个句子。在下一个文本框(textBox3)中,用户输入一个单词。然后用户单击一个按钮,第三个文本框(textBox2)检查输入的单词是否与句子中的单词匹配。我不能使用任何"捷径",我只能走很长的路(没有比较或类似的东西)。以下是目前为止的内容:

 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"上)。

c#需要帮助查找输入句子中的匹配词

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

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

如果不使用正则表达式或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++;
}

试试regExp

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
}

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

   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。你的老师可能应该想出一个更好的练习。你真的需要亲力亲为。

你所做的就是每次循环遍历整个句子。检查每个单词,包括最后一个单词和textBox2。每个单词的文本都在被更改。你看到的是最后一个单词的结果。

    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