title: Split string into list of N-length strings using LINQ
body: <p>I know the concept of String.Split has been addressed before with a multitude of different approaches, but I am specifically interested in a LINQ solution to this question.</p>
  
  <p>I've attempted to write an extension class to handle the split, but both attempts have some major issues. So for the following:</p>
  
  <pre><code>string s = "ABCDEFGHIJKLMNOPQRSTUVWX";
  var results = s.SplitEvery(4);
  </code></pre>
  
  <p>I would want a list like:
  { "ABCD", "EFGH", "IJKL", "MNOP", "QRST", "UVWX" }</p>
  
  <p>Here is my extension class:</p>
  
  <pre><code>public static class Extensions
  {
      public static List&lt;string&gt; SplitEvery(this string s, int n)
      {
          List&lt;string&gt; list = new List&lt;string&gt;();
  
          var Attempt1 = s.Select((c, i) =&gt; i % n== 0 ? s.Substring(i, n) : "|").Where(x =&gt; x != "|").ToList();
  
          var Attempt2 = s.Where((c, i) =&gt; i % n== 0).Select((c, i) =&gt; s.Substring(i, n)).ToList();
  
          return list;
      }
  }
  </code></pre>
  
  <p>Attempt 1 inserts a dummy string "|" every time the condition isn't met, then removes all instances of the dummy string to create the final list. It works, but creating the bad strings seems like an unnecessary extra step. Furthermore, this attempt fails if the string isn't evenly divisible by n.</p>
  
  <p>Attempt 2 was me trying to select only substrings where the index was divisible by N, but the 'i' value in the Select statement doesn't correspond to the 'i' value in the Where statement, so I get results like: { "ABCD", "BCDE", etc... }</p>
  
  <p>I feel like I'm close to a good solution, but could use a helpful nudge in the right direction. Any suggestions?</p>
  
  <p><strong>[Edit]</strong></p>
  
  <p>I ended up going with a combination of suggestions to handle my string-splitter. It might not be the fastest, but as a newbie to LINQ, this implementation was the most succinct and easy for me to understand.</p>
  
  <pre><code>public static List&lt;string&gt; SplitEvery(this string s, int size)
  {
      return s.Select((x, i) =&gt; i)
          .Where(i =&gt; i % size == 0)
          .Select(i =&gt; String.Concat(s.Skip(i).Take(size))).ToList();
  }
  </code></pre>
  
  <p>Thanks for all the excellent suggestions.</p>