找两个string[ ]里不同的元素

方法 1:
string[] strListOne = new string[] {"Type", "Parent Id","Status"};

string[] strListTwo =  new string[] {"Type", "Parent Id"};

foreach (string item in strListOne)

{             

  if (!strListTwo.Contains(item))

  {                 

    Console.WriteLine(item);

      }

 }

方法 2:

List<string> strList = newList<string>() { };   //如果你不确定不同的数据有几个,就用集合接收

string[] strListOne = newstring[] {"Type", "Parent Id","Status", "ID"};    

string[] strListTwo = newstring[] {"Type", "Parent Id"};

for (int i = 0; i < strListOne.Length; i++)

{            

  for (int j = 0; j < strListTwo.Length; j++)

      {                 

    if (!strListOne[i].Equals(strListTwo[j]))

            {              

        if (j == strListTwo.Length - 1)

        {

           strList.Add( strListOne[i]);

                    }              

        else

          break;

                }

      }

foreach (var item in strList)

 {          

  Console.WriteLine(item);

}

Console.ReadLine();

 

 

posted on 2014-03-27 14:02  KristyGuo  阅读(451)  评论(0编辑  收藏  举报

导航