string[] arr1={"11","33","2","5555","9999","10"};
string[] arr2={"11","9999","1","2"};
List<string> arr=new List<string>();
foreach(string c1 in arr1)
{
arr.Add(c1);
}
foreach (string c2 in arr2)
{
if (!arr1.Contains(c2))
{
arr.Add(c2);
}
}
foreach(string c3 in arr)
{
Console.WriteLine(c3);
}
Console.ReadLine();
class Program
{
static void Main(string[] args)
{
string[] arr1 = { "11", "33", "2", "5555", "9999" };
string[] arr2 = { "11", "9999" };
string[] arr3 = null;
List<string> list = new List<string>();
IEnumerable<string> arrs = arr1.Except(arr2);
foreach (string arr in arrs)
{
list.Add(arr);
}
arr3 = list.ToArray();
foreach (string a in arr3)
{
Console.Write("{0} ", a);
}
Console.ReadKey();
}
}
一句话的事情啊(C# 3.0以上):
string[] arr1 = { "11", "33", "2", "5555", "9999" };
string[] arr2 = { "11", "9999" };
string[] arr3 = arr1.Except(arr2).ToArray();
string[] arr1 = { "11", "33", "2", "5555", "9999" };
string[] arr2 = { "11", "9999","44" };
我要的到string[] arr3={"33","2","44","5555"}?
string[] arr3=arr1.Except(arr2).ToArray();还行吗? 求解释!!!
@O|(: 如果你想写的很酷而不是很迫切的要高性能,那么依然可以一句话:
string[] arr3 = arr1.Union(arr2).Except(arr1.Intersect(arr2)).ToArray();
这样效率稍微有点低(影响不大),高效的写法就是两重循环。
@水牛刀刀: o thak you!~.~
感谢
用linq to object实现只需要一个方法就可以了,相当的简单:string[] arr3 = arr1.Except(arr2).ToArray()。