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?
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.
Lists are one of the most common data structures in Python, and they are often used to hold strings.
You can use SkipWhile
and TakeWhile
var newList = list.SkipWhile(line => !line.Contains("keyword")) .Skip(1) .TakeWhile(line => !line.Contains("another keyword")) .ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With