Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote an extension method for this purpose which works on any <code>IEnumerable&lt;T&gt;</code>.</p> <pre><code>/// &lt;summary&gt; /// Splits the specified IEnumerable at every element that satisfies a /// specified predicate and returns a collection containing each sequence /// of elements in between each pair of such elements. The elements /// satisfying the predicate are not included. /// &lt;/summary&gt; /// &lt;param name="splitWhat"&gt;The collection to be split.&lt;/param&gt; /// &lt;param name="splitWhere"&gt;A predicate that determines which elements /// constitute the separators.&lt;/param&gt; /// &lt;returns&gt;A collection containing the individual pieces taken from the /// original collection.&lt;/returns&gt; public static IEnumerable&lt;IEnumerable&lt;T&gt;&gt; Split&lt;T&gt;( this IEnumerable&lt;T&gt; splitWhat, Func&lt;T, bool&gt; splitWhere) { if (splitWhat == null) throw new ArgumentNullException("splitWhat"); if (splitWhere == null) throw new ArgumentNullException("splitWhere"); return splitIterator(splitWhat, splitWhere); } private static IEnumerable&lt;IEnumerable&lt;T&gt;&gt; splitIterator&lt;T&gt;( IEnumerable&lt;T&gt; splitWhat, Func&lt;T, bool&gt; splitWhere) { int prevIndex = 0; foreach (var index in splitWhat .Select((elem, ind) =&gt; new { e = elem, i = ind }) .Where(x =&gt; splitWhere(x.e))) { yield return splitWhat.Skip(prevIndex).Take(index.i - prevIndex); prevIndex = index.i + 1; } yield return splitWhat.Skip(prevIndex); } </code></pre> <p>For example, in your case, you can use it like this:</p> <pre><code>var arr = new string[] { "foo", "bar", "jar", "\r", "a", "b", "c", "\r", "x", "y", "z", "\r" }; var results = arr.Split(elem =&gt; elem == "\r"); foreach (var result in results) Console.WriteLine(string.Join(", ", result)); </code></pre> <p>This will print:</p> <pre><code>foo, bar, jar a, b, c x, y, z </code></pre> <p>(including a blank line at the end, because there is a <code>"\r"</code> at the end of your collection).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload