I have a list string:
List<string> Entries = new List<string>();
Entries.Add("The quick brown fox jumps over the lazy dog");
Entries.Add("Lorem ipsum dolor sit amet");
Entires.Add("Consetetur sadipscing elitr");
Can I search for something like "fox"
in the list and get the whole line ( "The quick brown fox jumps over the lazy dog"
) returned?
Entries.Where(e=>e.Contains("fox")).FirstOrDefault()
Step 1: You can iterate over Entries
using foreach
loop.
Step 2: invoke Contains()
method on every loop item to check required string (fox)
.
foreach (var item in Entries)
{
if (item.Contains("fox"))
Console.WriteLine(item); //item found
}
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.