【问题标题】:Comparing words in two richtextbox to find difference?比较两个richtextbox中的单词以发现差异?
【发布时间】:2014-04-02 12:20:08
【问题描述】:

我有三个RichTextBoxes。我想将RichTextbox1的所有单词与Richtextbox2一一比较,以空格或逗号为分隔符。

如果它们相同,则什么也不做,如果不同,则将文本突出显示为某种颜色并将其保存在RichTextBox3

我的循环有点问题。

【问题讨论】:

  • 如果您尝试生成两个文本序列的差异,您可能需要考虑差异算法:en.wikipedia.org/wiki/Diff 或其变体之一

标签: vb.net loops compare richtextbox highlight


【解决方案1】:

说明

首先我们将声明一些变量来缩短我们的写作工作。然后我们将使用 For Each 命令。

在这里,我们将进行两轮扫描,首先是Richtextbox1,它不在Richtextbox2 中,反之亦然。不同的字符将不断添加到变量diff1diff 2 中,最后我们将在RichTextbox3 中编译它们。

它将适用于diff 算法。

代码与示例

Dim txt1(RichTextBox1.Text.Split(" ").Length) As String
Dim txt2(RichTextBox2.Text.Split(" ").Length) As String
txt1 = RichTextBox1.Text.Split(" ")
txt2 = RichTextBox2.Text.Split(" ")
Dim diff1 As String = "" 'Differences between 1 and 2
Dim diff2 As String = "" 'Differences between 2 and 1
For Each diff As String In txt1
   If Array.IndexOf(txt2, diff.ToString) = -1 Then
        diff1 += diff.ToString & " "
   End If
Next
For Each diff As String In txt2
   If Array.IndexOf(txt1, diff.ToString) = -1 Then
        diff2 += diff.ToString & " "
   End If
Next
    RichTextbox3.Text = diff1 & diff2
End Sub

希望它能完美运行!

【讨论】:

  • 是的,这非常有效,非常感谢您的帮助...我真的很感激。但是我如何着色或突出显示richtextbox1 中与richtextbox2 不同的文本。我写了一个格式化文本的函数。我只需要将它应用于所有不同的单词。 @Phillip Trelford:我看到算法将尝试它......非常感谢你的链接.. :) :)
  • 尝试以下操作:RichTextBox1.[Select](diff1) RichTextBox1.SelectionColor = Color.Red
  • 选择不带字符串,它需要布尔值或整数,对吗?
  • 有人可以帮我给文字上色吗??
  • @VineetKamath,我没有看到你的评论。我很忙。我只处理你的问题。请稍候:)
【解决方案2】:

有人可以帮我给文字上色吗? – Vineet Kamath 3 月 1 日 17:30

如果您想为文本着色或突出显示,您只需要 (1) 查找并选择单词/字符串和 (2) 设置文本属性。

尝试这样修改Error404的代码:

Dim diffPosition as integer ' Set where beging to find and select in RichTextBox
diffPosition = 1 ' Initialize

For Each diff As String In txt1
   If Array.IndexOf(txt2, diff.ToString) = -1 Then
        diff1 += diff.ToString & " "
        With RichTextBox1
             .Find(diff, diffPosition, RichTextBoxFinds.None) ' Find and select diff in RichTextBox1 starting from position diffPosition in RichtextBox1
             .SelectionFont = New Font(.Font, FontStyle.Bold) ' Set diff in Bold
             .SelectionColor = Color.Blue ' Set diff in blue instead of black
             .SelectionBackColor = Color.Yellow ' highlight in yellow
        End With
   End If
   diffPosition = diffPosition + Len(diff) ' re-Initialize diffPostion to avoid to find and select the same text present more than once
Next

diffPosition = 1 ' re-Initialize for RichTextBox2

For Each diff As String In txt2
   If Array.IndexOf(txt1, diff.ToString) = -1 Then
        diff2 += diff.ToString & " "
        With RichTextBox2
             .Find(diff, diffPosition, RichTextBoxFinds.None) ' Find and select diff in RichTextBox2 starting from position diffPosition in RichtextBox2
             .SelectionFont = New Font(.Font, FontStyle.Bold) ' Set diff in Bold
             .SelectionColor = Color.Blue ' Set diff in blue instead of black
             .SelectionBackColor = Color.Yellow ' highlight in yellow
        End With
   End If
   diffPosition = diffPosition + Len(diff) ' re-Initialize diffPostion to avoid to find and select the same text present more than once
Next
    RichTextbox3.Text = diff1 & diff2

代码应找到并选择“diff”,设置粗体样式,将每个字母的颜色设置为蓝色(而不是黑色)并以黄色突出显示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多