1

我逐字比较richTextBox1 和richTextBox2。

// collect words from ritchtextbox1
String[] test = richtextbox1.text.Split(" ", StringSplitOptions.RemoveEmptyEntries);

// find each word test to richtextbox2
// if it is found then change back color of particular word to green of the 
// else change back color of particular word to red in richtextbox1

test = richtextbox2.text.Split(" ", StringSplitOptions.RemoveEmptyEntries);

// find each word in test to richtextbox1
// if it is found then change back color of particular word to green of the 
// else change back color of particular word to red in richtextbox2

任何人都可以帮助我编写代码我的语法有点差。

我参考了mateusz代码

String[] test = richtextbox1.text.Split(" ", StringSplitOptions.RemoveEmptyEntries); 
String[] test2 = richtextbox2.text.Split(" ", StringSplitOptions.RemoveEmptyEntries);

bool wordNotFound=false;

for (int i=0;i<test.lenght;i++)
 for (int j=0;j<test2.length;j++){
   if (test[i].equals(test2[j])){
         wordNotFound=true
          break;
          }
     else wordNotFound=true;
  }

//// 这里我需要改变特定单词的颜色而不是整个 ritchtextbox

if (wordNotFound) richTextBox1.BackColor = Color.Red;
    else richTextBox1.BackColor = Color.Green;

我没有比较两个文本框,我验证了两边是否存在的每个单词。就像拼写检查将字典作为一个richtextbox1。Richtextbox2 中的拼写检查 [有效单词]。反之亦然...

4

4 回答 4

2
var validWords = new HashSet<string>(new string[] { "a","c","e" });
string[] wordsToCheck = new string[] { "a", "b", "c", "d", "e" };

var result = wordsToCheck.Select(w => new
                {
                    Word = w,
                    IsValid = validWords.Contains(w)
                })
                .ToList();

如果您只对所有单词是否有效感兴趣,您可以通过以下方式进行简单检查

var isOK = wordsToCheck.All(w => validWords.Contains(w));

PS:当然new string[]{}s要换成rtb.Split(....)*

于 2012-12-12T14:11:11.290 回答
1

如果单词的顺序相同,为什么还要费心拆分呢?如果可以的话,我会这样做:

if (richtexbox1.text.equals(richtexbox1.text)){
 richTextBox1.BackColor = Color.Green;
} else {
 richTextBox1.BackColor = Color.Red;
}

如果不是,并且您想查找两个文本框是否包含相同的单词但顺序不同,则:

    String[] test = richtextbox1.text.Split(" ", StringSplitOptions.RemoveEmptyEntries); 
    String[] test2 = richtextbox2.text.Split(" ", StringSplitOptions.RemoveEmptyEntries);

    bool wordNotFound=false;

    for (int i=0;i<test.lenght;i++)
     for (int j=0;j<test2.length;j++){
       if (test[i].equals(test2[j])){
             wordNotFound=false;
              break;
              }
         else wordNotFound=true;
      }

   if (wordNotFound) richTextBox1.BackColor = Color.Red;
        else richTextBox1.BackColor = Color.Green;
于 2012-12-12T12:42:04.330 回答
0
 String[] test = richtextbox1.text.Split(" ", StringSplitOptions.RemoveEmptyEntries); 
    String[] test2 = richtextbox2.text.Split(" ", StringSplitOptions.RemoveEmptyEntries);

给出错误;

错误 1 ​​'string.Split(string[], System.StringSplitOptions)' 的最佳重载方法匹配有一些无效参数 C:\Users\Saad\Documents\Visual Studio 2012\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Form1.cs 60 29 WindowsFormsApplication5

错误 2 参数 1:无法从 'string' 转换为 'string[]' C:\Users\Saad\Documents\Visual Studio 2012\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Form1.cs 60 53 WindowsFormsApplication5

于 2016-05-12T13:03:50.043 回答
-1

您可以将 textbox1 中的每个单词与 textbox2 进行比较

for(int i = 0; i < stringarray1.length; i++)
{
 for(int j = 0; j < stringarray2.length; j++)
 {
   if(stringarray1[i] == stringarray2[j])
     // we have a match
 }
}
于 2012-12-12T12:38:55.273 回答