比较字符串并获得c#中的distinct

时间:2014-11-11 06:46:04

标签: c# linq

示例:  string a =“6,9”;  string b =“5,9”;

比较以上我需要独特的元素

结果:

α= 6; B = 5;

3 个答案:

答案 0 :(得分:3)

string a= "6,9"; string b= "5,9";
string uniquea = string.Join(",", a.Split(',').Except(b.Split(',')));
string uniqueb = string.Join(",", b.Split(',').Except(a.Split(',')));

这会在ab内保留重复内容。

答案 1 :(得分:0)

string a= "6,9"; string b= "5,9";
char[] splitters = new[] { ',', ' '};
var aList = a.Split(splitters);
var bList = b.Split(splitters);
var uniqueA = aList.Except(bList).ToList();
var uniqueB = bList.Except(aList).ToList();

答案 2 :(得分:0)

string[] assets, allassets = null;
int[] list1, list2, removed_list, added_list = null;

assets = a.Split(',');
list1 = Array.ConvertAll(assets, x => int.Parse(x))

allassets = b.Split(',');
list2 = Array.ConvertAll(allassets, x => int.Parse(x));

removed_list = list2.Where(x => !list1.Contains(x)).ToArray(); // which gives =>a
added_list = list1.Where(x => !list2.Contains(x)).ToArray(); // which gives =>b

我完成了这个,你们怎么说?