比较两个字符串并突出显示不同的

时间:2018-02-23 14:09:40

标签: asp.net vb.net visual-studio

我有两个字符串是通过文本框接收的,在vb.net中是否有办法比较两个字符串,并以不同的颜色突出显示差异?

到目前为止,我所做的是:

Dim txt1(textbox1.Text.Split(CType(" ", Char())).Length) As String
Dim txt2(textbox2.Text.Split(CType(" ", Char())).Length) As String
txt1 = textbox1.Text.Split(CType(" ", Char()))
txt2 = textbox2.Text.Split(CType(" ", Char()))

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
Response.Write("Difference in First To Second: " & diff1 & vbNewLine & "Difference in Second To First: " & diff2)

但是我试图在第二个文本框中突出显示不同的单词,这些单词与第一个不同。

帮助表示赞赏:)

1 个答案:

答案 0 :(得分:3)

不幸的是,无法直接在TextBox中执行突出显示,但您可以在外面使用HTML执行突出显示。

这是我为此写的一个小功能。它使用Regex来迭代每个单词,并将HTML应用于不同的单词:

Protected Function HighlightDifferences(ByVal Input As String, ByVal CompareTo As String, ByVal Color As String) As String
    Dim Prefix As String = "<span style=""color: " & Color & """>"
    Dim Suffix As String = "</span>"

    Dim Result As String = Input
    Dim WordRegex As New Regex("\S+", RegexOptions.Compiled)

    Dim StartIndex As Integer = 0
    Dim m As Match = WordRegex.Match(Result)

    While m IsNot Nothing AndAlso m.Success = True
        StartIndex = m.Index + m.Length

        If Regex.IsMatch(CompareTo, Regex.Escape(m.Value)) = False Then
            Result = Result.Insert(m.Index, Prefix).Insert(Prefix.Length + m.Index + m.Length, Suffix)
            StartIndex += Prefix.Length + Suffix.Length
        End If

        If StartIndex < Result.Length Then
            m = WordRegex.Match(Result, StartIndex)
        Else
            m = Nothing
        End If
    End While

    Return Result
End Function

用法:

Dim Differences As String = HighlightDifferences(TextBox2.Text, TextBox1.Text, "#FF0000")
'Do something with the Differences variable, for instance output it to the page.

请记住,这只是检查第一个字符串中的每个单词是否都存在于第二个字符串中,这意味着它并不关心 单词所在的位置也不是发生了多少次

为了进行完整的差异检查(例如Stack Overflow的编辑修订系统),您将面临LCS problem。编写Diff算法很复杂,所以如果这是你想要的,那么最好找到一个已经存在的Diff算法的.NET实现,而不是尝试编写自己的算法。

页面示例(带截图)

Page.aspx(仅限正文):

<form id="form1" runat="server">
    <div>
        <h1 style="text-align: center">Text comparison tool</h1>
        <p style="text-align: center">Initial text:</p>
        <asp:TextBox ID="TextBox1" runat="server" Height="150px" Width="50%" TextMode="MultiLine" style="margin-left: 25%; margin-right: 25%">The quick brown fox jumps over the lazy dog</asp:TextBox><br/>

        <p style="text-align: center">Text to compare to:</p>
        <asp:TextBox ID="TextBox2" runat="server" Height="150px" Width="50%" TextMode="MultiLine" style="margin-left: 25%; margin-right: 25%">The quick green fox jumps over the lazy cat</asp:TextBox>

        <p style="text-align: center;"><asp:Button ID="Button1" runat="server" Text="Find differences" /></p>

        <% 
            If Session("TextDiff") IsNot Nothing Then
        %>
        <div>
            <h1 style="text-align: center">Highlighted differences:</h1>
            <p style=" width: 50%; margin-left: 25%; margin-right: 25%"><%= Session("TextDiff") %></p>
        </div>
        <%
                Session.Remove("TextDiff")
            End If
        %>
    </div>
</form>

<强> Page.aspx.vb:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Session("TextDiff") = HighlightDifferences(TextBox2.Text, TextBox1.Text, "#FF0000")
End Sub

Protected Function HighlightDifferences(ByVal Input As String, ByVal CompareTo As String, ByVal Color As String) As String
    Dim Prefix As String = "<span style=""color: " & Color & """>"
    Dim Suffix As String = "</span>"

    Dim Result As String = Input
    Dim WordRegex As New Regex("\S+", RegexOptions.Compiled)

    Dim StartIndex As Integer = 0
    Dim m As Match = WordRegex.Match(Result)

    While m IsNot Nothing AndAlso m.Success = True
        StartIndex = m.Index + m.Length

        If Regex.IsMatch(CompareTo, Regex.Escape(m.Value)) = False Then
            Result = Result.Insert(m.Index, Prefix).Insert(Prefix.Length + m.Index + m.Length, Suffix)
            StartIndex += Prefix.Length + Suffix.Length
        End If

        If StartIndex < Result.Length Then
            m = WordRegex.Match(Result, StartIndex)
        Else
            m = Nothing
        End If
    End While

    Return Result
End Function

<强>结果:

  

Text diff example