Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab a portion of List<string>

Tags:

c#

linq

I have a List, looking like this:

some headline content a subheadline containing the keyword  1 2015-05-05 some data 2 2015-05-05 some data 3 2015-05-05 some data some content a subheadline containing another keyword  useless stuff 

So now I want to grab all the stuff between "keyword" and "another keyword". Maybe I should find the index of "keyword" and "another keyword" and use .GetRange(), but is there a more elegant way to do this with e.g. LINQ?

like image 912
DaFunkyAlex Avatar asked Aug 17 '15 10:08

DaFunkyAlex


People also ask

How do you extract part of a list in python?

As well as using slicing to extract part of a list (i.e. a slice on the right hand sign of an equal sign), you can set the value of elements in a list by using a slice on the left hand side of an equal sign. In python terminology, this is because lists are mutable objects, while strings are immutable.

Can a list hold a string?

Lists are one of the most common data structures in Python, and they are often used to hold strings.


1 Answers

You can use SkipWhile and TakeWhile

var newList = list.SkipWhile(line => !line.Contains("keyword"))                   .Skip(1)                   .TakeWhile(line => !line.Contains("another keyword"))                   .ToList(); 
like image 184
Eser Avatar answered Oct 19 '22 23:10

Eser