0

I recently wrote the following code to split a css file into chunks:

Dim seg = css.Take(css.Length / segmentCount).TakeWhile(Function(x) x <> "}"c).Take(1)

The idea being that I take a chunk of the css then continue taking until i hit a closing brace and then take the brace as well.

Obviously this didn't work and i realised why it doesnt almost immediately (The Take needle isnt maintained between the take calls).

My question is is there way to write this idea as a LINQ query efficiently considering that they string might be 300,000 characters or so long.

(I ended up using a combination of SubString and IndexOf for this but a one liner in LINQ would be interesting)

Calin Leafshade
  • 1,195
  • 1
  • 12
  • 22
  • As an aside, use `css.Length \ segmentCount` if you want integer division, otherwise it returns a `Double`. – Tim Schmelter May 13 '15 at 11:00
  • 1
    Also, there's nothing more efficient than string methods. – Tim Schmelter May 13 '15 at 11:01
  • I'm aware that the string method is the most efficient, yea. But academically I would like to know if a "AndTake" option exists. Also I didn't know about "\" as an operator. VB is weird. – Calin Leafshade May 13 '15 at 11:03
  • \ is what / is in C# by default. If you want to "maintain the `Take` needle" you have to `Skip` the part that you have taken last time. But as you already might know, that's not really efficient. You're looping the string multiple times. – Tim Schmelter May 13 '15 at 11:03
  • You're looking for [`TakeUntilIncluding`](http://stackoverflow.com/a/6817553/861716). – Gert Arnold May 13 '15 at 15:09

0 Answers0