如何区分2组结果并在C#中提供任何颜色? [英] How differentiate 2 set of results and provide any color in C#?

查看:74
本文介绍了如何区分2组结果并在C#中提供任何颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个字符串如下



1.

I have 2 strings like below

1.

name=name1 address=address1



2.


2.

name=name2 address=address1





如何为name1提供defferntiate并提供颜色name2被发现不同或更新。



我尝试过:



我尝试过以下代码正常工作但在label2中获取重复值





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

推荐答案

GitHub - mmanela / diffplex:DiffPlex是Netstandard 1.0 C#库,用于生成文本差异。 [ ^ ]



如何显示差异将取决于哪个您正在使用的UI框架。存储库包含一个ASP.NET样本,可以帮助您入门。
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.


如果行总是被格式化为示例,您可以使用 String来拆分文本。 .Split 然后成对比较每个字符串部分。如果它们不同,则用彩色渲染它们。



如果您编写Windows控制台应用程序,可以使用 Console.ForegroundColor
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

<设置颜色br />


好的,谢谢,我得到了完美的结果
Ok thank you i got the perfect result


这篇关于如何区分2组结果并在C#中提供任何颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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