比较两个RichTextBox?

时间:2012-12-12 12:30:32

标签: c# compare richtextbox

我正在逐字逐句地比较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 个答案:

答案 0 :(得分: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[]{}应替换为rtb.Split(....) *

答案 1 :(得分: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;

答案 2 :(得分: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

答案 3 :(得分:-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
 }
}