Compare two strings and highlight changes using C# and VB.Net in ASP.Net

Last Reply 25 days ago By pandeyism

Posted 25 days ago

How to compare Word to Word in c#

old Text:

Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business as a global company. Mr. Dhinna brings to the board significant commercial transaction experience from his Kissinger Associates role till 25/06/2016, as well as domestic and international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving related to global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc.

New Text:

Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business as a global company. Mr. Dhinna brings to the board significant commercials transaction less experience from his Kissinger Associates role till 25/06/2018, as well as domestic and international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving could not be to global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc.

Desire Result:

Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business as a global company. Mr. Dhinna brings to the board significant commercial commercials transaction less experience from his Kissinger Associates role till 25/06/2016 2018, as well as domestic and international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving related could not be to global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc.

My code is:

protected void btnsubmit_Click(object sender, EventArgs e)
{
    string oldText = txtold.Text;
    string newText = txtnew.Text;

    string[] oldt = oldText.Split(' ');
    string[] newt = newText.Split(' ');

    for (int i = 0; i < oldt.Length; i++)
    {
        if (oldt[i] == newt[i])
        {
            lblResult.Text += oldt[i].ToString() + " ";
        }
        else
        {
            lblResult.Text += "<strike><font color='red'>" + oldt[i].ToString() + "</font></strike>" + "<u><font color='blue'>" + newt[i].ToString() + "</font></u>" + " ";
        }
    }
}

 

You are viewing reply posted by: pandeyism 25 days ago.
Posted 25 days ago

Hi naresh1994,

Refer below sample.

Use regular expression and stringcollection check whether words are containing or not.

Refer below sample for better understand.

HTML

<asp:Label ID="lblMessage" runat="server" />

Namespaces

C#

using System.Collections.Specialized;

VB.Net

Imports System.Collections.Specialized

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string message1 = "Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business";
    message1 += "as a global company. Mr. Dhinna brings to the board significant commercial transaction experience from his Kissinger Associates role till 25/06/2016, as well as domestic and";
    message1 += "international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving related to";
    message1 += "global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc.";

    string message2 = "Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business";
    message2 += "as a global company. Mr. Dhinna brings to the board significant commercials transaction less experience from his Kissinger Associates role till 25/06/2018, as well as domestic and";
    message2 += "international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving could not be to";
    message2 += "global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc.";
    StringCollection stringCollectionmessage1 = new StringCollection();
    StringCollection stringCollectionmessage2 = new StringCollection();
    stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, @"\b(\w+)(\W){1,2}"));
    stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, @"\b(\w+)(\W){1,2}"));
    for (int i = 0; i < stringCollectionmessage1.Count; i++)
    {
        if (!stringCollectionmessage1.Contains(stringCollectionmessage2[i]))
        {
            lblMessage.Text += "<font color='Red'>" + stringCollectionmessage2[i].ToString() + "</font>" + " ";
        }
        else
        {
            lblMessage.Text += stringCollectionmessage1[i].ToString() + " ";
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
    Dim message1 As String = "Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business"
    message1 += "as a global company. Mr. Dhinna brings to the board significant commercial transaction experience from his Kissinger Associates role till 25/06/2016, as well as domestic and"
    message1 += "international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving related to"
    message1 += "global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc."
    Dim message2 As String = "Mr. Dhinna's broad international experience provides the board of directors with his unique insights related to Blockbuster strategy, operations and business"
    message2 += "as a global company. Mr. Dhinna brings to the board significant commercials transaction less experience from his Kissinger Associates role till 25/06/2018, as well as domestic and"
    message2 += "international public policy experience. Mr. Dhinna has extensive knowledge in a number of important areas, including innovative problem-solving could not be to"
    message2 += "global risks and opportunities, particularly with regard to China and Japan. He is also ex CFO of ABC Inc."
    Dim stringCollectionmessage1 As StringCollection = New StringCollection()
    Dim stringCollectionmessage2 As StringCollection = New StringCollection()
    stringCollectionmessage1.AddRange(System.Text.RegularExpressions.Regex.Split(message1, "\b(\w+)(\W){1,2}"))
    stringCollectionmessage2.AddRange(System.Text.RegularExpressions.Regex.Split(message2, "\b(\w+)(\W){1,2}"))

    For i As Integer = 0 To stringCollectionmessage1.Count - 1

        If Not stringCollectionmessage1.Contains(stringCollectionmessage2(i)) Then
            lblMessage.Text += "<font color='Red'>" & stringCollectionmessage2(i).ToString() & "</font>" & " "
        Else
            lblMessage.Text += stringCollectionmessage1(i).ToString() & " "
        End If
    Next
End Sub

Output

Mr Dhinna ' s broad international experience provides the board of directors with his unique insights related to Blockbuster strategy operations and businessas a global company Mr Dhinna brings to the board significant commercials transaction less from his Kissinger Associates role till 25 / 06 / 2016 2018 well as domestic andinternational public policy experience Mr Dhinna has extensive knowledge in a number of important areas including innovative problem - solving related could not be opportunities particularly with regard to China and Japan He is also ex CFO of ABC Inc .