Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 2 strings like below

1.
name=name1 address=address1

2.
name=name2 address=address1


how to defferntiate and provide color for name1 and name2 which are found as different or updated .

What I have tried:

I have tried with the below code working fine but getting duplicate values in label2

string[] a1 = { "name=name1", "address=address1", "mobilenumber=mobilenumber1", "area=area1" };

           string[] a2 = { "name=name1", "address=address12", "mobilenumber=mobilenumber1", "area=area1" };
           StringBuilder txtAppend = new StringBuilder();
           StringBuilder txtAppend2 = new StringBuilder();

           foreach (string str in a1)
           {
               txtAppend.Append(str +"<br />");
               Label1.Text = txtAppend.ToString();
           }
           foreach (string str in a2)
           {

               txtAppend2.Append(str + "<br />");
               if (!a1.Contains(str))
               {

                   txtAppend2.Append("<span style='color:red;'>" + str + "</span><br />");

               }

               Label2.Text = txtAppend2.ToString();
           }


Like below
name=name1                   name=name1
address=address1             address=address12  **** repeated elemnt with black color
mobilenumber=mobilenumber1   address=address12  **** this is red 
area=area1                   mobilenumber=mobilenumber1
                             area=area1


could you please help me that how to avoid repeated elemnts
Posted
Updated 8-Jun-17 19:15pm
v2

GitHub - mmanela/diffplex: DiffPlex is Netstandard 1.0 C# library to generate textual diffs.[^]

How you display the differences will depend on which UI framework you're using. The repository includes a sample for ASP.NET which should get you started.
 
Share this answer
 
If the lines are always formatted as the examples you could for instance split the text using String.Split and then compare each string part pairwise. If they differ you render them in color.

If you write a Windows console application you can set the color using Console.ForegroundColor
 
Share this answer
 
Comments
DGKumar 8-Jun-17 15:06pm    
Hi
I need to provide colors in jquery which data is mismatch form one and other but those 2 strings should be display in 2 divs at ui, just needs to provide color which is updated to identify.
DGKumar 9-Jun-17 0:54am    
i have tried with below code
string[] a1 = { "name=name1", "address=address1", "mobilenumber=mobilenumber1", "area=area1" };

string[] a2 = { "name=name1", "address=address12", "mobilenumber=mobilenumber1", "area=area1" };
StringBuilder txtAppend = new StringBuilder();
StringBuilder txtAppend2 = new StringBuilder();

foreach (string str in a1)
{
txtAppend.Append(str +"");
Label1.Text = txtAppend.ToString();
}
foreach (string str in a2)
{

txtAppend2.Append(str + "");
if (!a1.Contains(str))
{

txtAppend2.Append("" + str + "");

}

Label2.Text = txtAppend2.ToString();
}

but i am getting duplicate values in lable2
Ok thank you i got the perfect result
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900