简体   繁体   中英

Check if List<string> exists

This is my code

List<string> list = new List<string>() { "bfd", "jy", "aaaaa", "ndnk", "ntfn", "gfm", "gfm", "mhgd5h", "srsr", "ntr", "mtmyt", "dmy", "mmy6", "ngn9d", "y6m1", "d8dm", "bbbbb", "tym", "dmj", "trsh", "tsr"};
List<string> test = new List<string>() {"aaaaa","bbbbb","ccccc","ddddd","eeeee","fffff","ggggg" };
           
foreach (var a in list)
{
    foreach (var i in test)
    {
        if (i.StartsWith(a) == false)
        {
            Console.WriteLine(i);
        }
    }
}

I want to output from the list and match in the test. If the test exists, then it will not be displayed. If there is no side output,But there may be a problem with my code, it will output the same value many times

The list contains aaaaa, according to logic, aaaaa should not be output

Try this:

      List<string> list = new List<string>(){"bfd", "jy", "aaaaa", "ndnk", "ntfn", "gfm", "gfm", "mhgd5h", "srsr", "ntr", "mtmyt", "dmy", "mmy6", "ngn9d", "y6m1", "d8dm", "bbbbb", "tym", "dmj", "trsh", "tsr"};

      List<string> test = new List<string>(){"aaaaa", "bbbbb", "ccccc", "ddddd", "eeeee", "fffff", "ggggg"};

        foreach (var a in list)
        {
            if (!test.Contains(a))
            {
                Console.WriteLine(a);
            }       
        }

There are simple solutions using LINQ. But since I assume this is an exercise, I will show you the detailed version using loops.

The problem is that the WriteLine is inside the inner loop. Therefore you will get many repetitions. Store the outcome of the test in a Boolean variable and place the display part after the inner loop.

foreach (var a in list)
{
    bool found = false;
    foreach (var i in test)
    {
        if (i.StartsWith(a))
        {
            found = true;
            break; // Exit the inner loop
        }
    }
    if (!found)
    {
        Console.WriteLine(a);  // We must output a here since i out of scope.
    }
}

Also, if you want to know whether a specific string is contained in the list, you should probably replace i.StartsWith(a) with i == a .


The code becomes easier to read, if you extract the list search into its own method

private bool IsInList(List<string> list, string item)
{
    foreach (string s in list)
    {
        if (s == item)
        {
            return true;
        }
    }
    return false;
}

Now, you don't need the Boolean variable any more

foreach (var a in list)
{
    if (!IsInList(test, a))
    {
        Console.WriteLine(a);
    }
}

I used !condition (pronounce: "not condition") instead of condition == false . It's terser.


I assume that this is an exercise. In production code you would simply use the built in method Contains of the list.

foreach (var a in list)
{
    if (!test.Contains(a))
    {
        Console.WriteLine(a);
    }
}

or even (as @mjwills suggests in his comment):

foreach (var a in list.Except(test)) {
    Console.WriteLine(a);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM