0

I'm experiencing a problem of further kind:

I have an array like {0xNN, 0xNN, 0xNN, 0xNN, 0xFF, 0xFF, 0xNN, 0xNN, 0xFF, 0xFF} where {0xFF, 0xFF} is a delimeter.

Is it possible to get an enumaration like {{0xNN, 0xNN, 0xNN, 0xNN} , {0xNN, 0xNN}} by using LINQ?

6
  • 2
    possible duplicate of How to split a byte array Commented Jul 5, 2012 at 14:41
  • @Mike That q's talking about a type of substring, not split. Commented Jul 5, 2012 at 14:44
  • @Mike Cant see LINQ or any other kind of lambdas there. Commented Jul 5, 2012 at 14:44
  • @IceCroft: if your question is simply "is it possible", then the answer is Yes. Perhaps you should show some code or context so that we may understand why this is restricted to "LINQ" (or please describe what you think LINQ is, because LINQ has nothing to do with lambda methods). Commented Jul 5, 2012 at 15:16
  • @sixlettervariables agreed, ofc its possible with just linq but, it would be difficult to avoid lots of pointless reiteration. Commented Jul 5, 2012 at 15:32

2 Answers 2

1

You could write an extension like this, to do the job in one pass with any length of delimiter.

public static class Extensions
{
    public static IEnumerable<IEnumerable<T>> Split<T>(
        this IEnumerable<T> source,
        IEnumerable<T> delimiter)
    {
       var delimiterList = delimiter.ToList();
       var outputBuffer = new List<T>();
       var m = 0;

       foreach(var item in source)
       {
           if item.Equals(delimiterList[m])
           {
               m++;

               if(m == delimiterList.Count)
               {
                  m = 0;

                  if (outputBuffer.Count > 0)
                  {
                      yield return outputBuffer;
                      outputBuffer = new List<T>();
                  }
               }
           }
           else
           {
               outputBuffer.AddRange(delimiterList.Take(m));

               if (item.Equals(delimiterList[0]))
               {
                   m = 1;
               }
               else
               {
                   m = 0;
                   outputBuffer.Add(item);
               }
           }              
       }

       outputBuffer.AddRange(delimiterList.Take(m)); 

       if (outputBuffer.Count > 0)
       {
           yield return outputBuffer;
       }
   }
}

Which you could use like this.

var joinedBytes = new byte[] { ... }
var delimiter = new byte[] { 0xFF, 0xFF }

var splitBytes = joinedBytes.Split(delimiter);
Sign up to request clarification or add additional context in comments.

1 Comment

@Akli, another good fix that got rejected. I've integrated your suggestion with a slight tweak.
-1

I don't think you can do it in LINQ, because your delimiter is defined by two elements of the collection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.