Home > Net >  C#: Check list for search term and print all matching values, or display Not found if no matching va
C#: Check list for search term and print all matching values, or display Not found if no matching va

Time:04-22

I'm trying to add a search option to my app. If the user's search term exists in a list (of strings), all matching elements from that list should be displayed. If not, "Not found" should be displayed. I have the for loop working, but I can't seem to find where the not found condition should go.

for (int i = 0; i<myList.Count; i  )
            {
                if (myList[i].Contains(search))
                {
                    WriteLine(myList[i]);
                } else
                {
                    WriteLine("not found");
                }
            }

CodePudding user response:

Since you want to get ALL of the matches, you need to keep a running list of the matches you found. After you have checked all items in your list, you can determine if there was none found by checking how many matches are in your list.

var matches = new List<string>();

for (int i = 0; i < myList.Count; i  )
{
    if (myList[i].Contains(search))
    {
        matches.Add(myList[i]);
    }
}

if (matches.Count == 0)
{
     WriteLine("not found");
}
else
{
    foreach (var match in matches)
    {
        WriteLine(match);
    }
}

If you do not need the matches afterwards, you could replace the list with an int and increment that when you find a match. If a match is found in the loop, write line the match. Then after you've checked all items in your list, you can check if your counter equals zero, if so write line "not found".

var counter = 0;

for (int i = 0; i < myList.Count; i  )
{
    if (myList[i].Contains(search))
    {
        WriteLine(myList[i]);
        counter  ;
    }
}

if (counter == 0)
{
    WriteLine("not found");
}

CodePudding user response:

1. You can use lambda for your problem.

Very simple, but don't forget to add using System.Linq. Lambda result (that contains all needed items) will be recorded in variable match and according to it size match.Count() you can print all of them or error if there is nothing to print.

var match = myList.Where(s => s.Contains(search));
if (match.Count() == 0) {
    Console.WriteLine("Not found!");
} else {
    foreach (var item in match) {
        Console.WriteLine(item);
    }
}

2. Good old for-loop.

Just add all the matching items to another list and then work with them like in lambda-case.

var match = new List<string>();
foreach (var item in myList) {
    if (item.Contains(search)) { 
        match.Add(item);
    }
}
if (match.Count() == 0){
    Console.WriteLine("Nothing found!");
} else {
    foreach (var item in match) {
        Console.WriteLine(item);
    }
}

All in all, main idea is to store a search result in another variable and then to work with it.

3. Found flag.

Another way is to use some bool flag and turn it to true, when we found something.

bool foundFlag = false;
foreach (var item in myList) {
    if (item.Contains(search)) {
        foundFlag = true;
        Console.WriteLine(item);
    }
}
if (!foundFlag){
    Console.WriteLine("Nothing found!");
}
  • Related