[英]c# find words in sequence in richtextbox
我有2個rich text boxes
。 第一個文本框包含輸入,第二個文本框將顯示找到的單詞的輸出
輸入:我的名字是umer,我的父親名字是waqar
輸出:
umer is found
my is found
name is found
is is found
father is found
my is found
輸出不是我想要的。 我想要下面這樣的輸出 :
my is found
name is found
is is found
umer is found
my is found
father is found
name is found
is is found
waqar is found
我的代碼是:
private void button1_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Contains("umer"))
richTextBox2.AppendText("\numer is found");
if (richTextBox1.Text.Contains("my"))
richTextBox2.AppendText("\nmy is found");
if (richTextBox1.Text.Contains("name"))
richTextBox2.AppendText("\nname is found");
if (richTextBox1.Text.Contains("is"))
richTextBox2.AppendText("\nis is found");
if (richTextBox1.Text.Contains("father"))
richTextBox2.AppendText("\nfather is found");
if (richTextBox1.Text.Contains("waqar"))
richTextBox2.AppendText("\nwaqar is found");
}
如果在每個單詞后都is found
您想要的is found
,則可以將if
列表替換為:
var words = richTextBox1.Text.Split(' ');
richTextBox2.Text = String.Join(words, " is found \n");
您可以使用linq獲得獨特的單詞:
string text = "my name is umer my father name is waqar"; var uniqueWords = text.Split(' ').GroupBy(x => x).Select(x=>x.Key); foreach (var value in uniqueWords) { richTextBox2.AppendText(value +" is found"); }
var result= text.Split(' ');
foreach (var value in result)
{
richTextBox2.AppendText(value +" is found");
}
這是您所需要的:
string[] words = richTextBox1.Text.Split(' ');
foreach (string searchString in words)
{
if (richTextBox1.Text.Contains(searchString))
{
richTextBox2.AppendText(searchString + " is found.\n");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.