mr.freeze
mr.freeze

Reputation: 14060

Select items from a list after the first occurrence of a character

Given a list like this:

["singing", "office", "1995>Work Photos", "Kevin", "and", "Emma", "Holiday Party>Karaoke"]

What's the most concise LINQ query to select the first element in the list that includes the character '>' and all elements after it. For the example above, I'd like this result:

["1995>Work Photos", "Kevin", "and", "Emma", "Holiday Party>Karaoke"]

I could obviously loop through and do it manually but it feels like there's an elegant one-liner out there. Here's the manual attempt:

var found = false;
var filtered = new List<string>();
foreach (var itm in list){
   if (itm.Contains(">")) found = true;
   if (found) filtered.Add(itm);
}
Console.WriteLine(filtered);

Upvotes: 2

Views: 405

Answers (1)

ocuenca
ocuenca

Reputation: 39326

I think you are seeking SkipWhile extension method:

var ar = new string []{ "singing", "office", "1995>Work Photos", "Kevin", "and", "Emma", "Holiday Party>Karaoke"};
var r= ar.SkipWhile(s => !s.Contains(">"));

Upvotes: 6

Related Questions