比较两个richtextbox中的单词以发现差异? [英] Comparing words in two richtextbox to find difference?

查看:37
本文介绍了比较两个richtextbox中的单词以发现差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个 RichTextBoxes.我想将 RichTextbox1Richtextbox2 的所有单词以空格或逗号作为分隔符一一进行比较.

I have three RichTextBoxes. I want to compare all the words of RichTextbox1 with Richtextbox2 one by one with space or comma as the delimiter.

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

If they are same do nothing, if not highlight the text to some color and save it in RichTextBox3.

我的循环有点问题.

推荐答案

说明

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

Explanation

First we will declare some variables to shorten our writing work. We'll then use the For Each command.

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

Here we will take two rounds to scan the differences, first of Richtextbox1 which is not in Richtextbox2 and vice versa. The different characters will keep adding to the variable diff1 and diff 2 and at the end we will compile both of them in 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

希望它能完美运行!

这篇关于比较两个richtextbox中的单词以发现差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆