jacobz
jacobz

Reputation: 3291

Search for specific text in list string and return whole line

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?

Upvotes: 0

Views: 1617

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

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
}

Upvotes: 2

AD.Net
AD.Net

Reputation: 13399

Entries.Where(e=>e.Contains("fox")).FirstOrDefault()

Upvotes: 5

Related Questions