Comparing words in two richtextbox to find difference?

后端 未结 2 1007
深忆病人
深忆病人 2021-01-16 00:08

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

2条回答
  •  孤城傲影
    2021-01-16 00:34

    Explanation

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

    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.

    It will work on the diff algorithm.

    Code And Example

    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
    

    Hope it would work perfectly!

提交回复
热议问题