简体   繁体   中英

Searching in a List, for a string; C#

I am trying to search a List of strings. I want to be able to return any found results that include information from a string. For example:

(I already have a list with values) List strings contains

"dog",
"cat",
"doghouse",
"doge",
"mouse"

I want a way to check a string, for example:

string dog = "dog"

And return the values:

"dog",
"doghouse",
"doge"

This is what I have so far, at least the portion I need help on.

Console.WriteLine("What would you like to search for (Title: Full Title; Author: first, last): ");

                search = Console.ReadLine();

                var results = bookList.Where(x => x.Contains(search)).ToList();

                if (results != null)

                {

                    Console.WriteLine(results);

                    //i--;

                    search = String.Empty;

                    clearvars results;

                    Console.WriteLine("Press Enter to continue");

                    Console.ReadLine();

                    continue;

                }

You can use the following.

var dog = "dog";
var result = YourList.FindAll(y => y.ToLower().Contains(dog.ToLower()));

你可以试试这个

var results = my list.Where(x => x.Contains(str)).ToList();

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