列表中的字符串比较

use*_*888 0 .net c#

我有两个字符串列表

l1 = {abc;xyz}  
l2 = {lmn,xyz,abc}  
Run Code Online (Sandbox Code Playgroud)

我想迭代这两个列表,看看l2是否包含l1中的所有元素,以及l1是否包含l2中的所有元素.
字符串的顺序无关紧要.请注意,字符串具有分隔符";"
我正在使用这两个for循环,但第二个for循环使索引超出范围.有一个更好的方法吗?

for (int i = 0; i < l1.Count; i++)
{
     if (l1[i].Contains(l2[i])) {
          Console.WriteLine("value {0} present in l1", l2[i]);
     }
     else {
          Console.WriteLine("value {0} is not present in l1", l2[i]);
     }
}

for (int i = 0; i < l2.Count; i++)
{
    if (l2[i].Contains(l1[i]))  {
        Console.WriteLine("value {0} present in l2", l2[i]);
    }
    else {
        Console.WriteLine("value {0} is not present in l2", l2[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

Sel*_*enç 6

您可以使用AllLINQ中的方法:

// check whether l2 contains all elements of l1
l1.All(l2.Contains)
// check whether l1 contains all elements of l2
l2.All(l1.Contains)
Run Code Online (Sandbox Code Playgroud)