Click here to Skip to main content
15,898,020 members
Home / Discussions / C#
   

C#

 
GeneralRe: Group Sequance patterns with Linq Pin
Member 121069269-Sep-16 2:28
Member 121069269-Sep-16 2:28 
SuggestionRe: Group Sequance patterns with Linq Pin
Richard Deeming9-Sep-16 4:15
mveRichard Deeming9-Sep-16 4:15 
GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP9-Sep-16 4:18
professional#realJSOP9-Sep-16 4:18 
GeneralRe: Group Sequance patterns with Linq Pin
Richard Deeming9-Sep-16 4:19
mveRichard Deeming9-Sep-16 4:19 
GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP9-Sep-16 4:25
professional#realJSOP9-Sep-16 4:25 
AnswerRe: Group Sequance patterns with Linq Pin
Member 121069269-Sep-16 2:26
Member 121069269-Sep-16 2:26 
AnswerRe: Group Sequance patterns with Linq Pin
Gerry Schmitz9-Sep-16 13:30
mveGerry Schmitz9-Sep-16 13:30 
AnswerRe: Group Sequance patterns with Linq Pin
BillWoodruff10-Sep-16 2:34
professionalBillWoodruff10-Sep-16 2:34 
C#
Attention Member 12106926: Please note the code shown here was written a few years ago, and has not been tested, recently. I would strongly advise you to use this code (if you find it useful) only as the basis for your own code, and, as always, to test thoroughly.
Well, Sir, I am reluctant to step up to the plate when heavy-hitters like Simmons and Deeming are in the ... I'll be lucky if I don't get brained by a curvew-ball, let alone strike-out with both of them, but, I like to try and create generic solutions to tasks like cleaning-up (getting rid of unwanted whatevers at the start and end of a collection), and parsing. Here's a generic parser based on Extension Methods that will parse the OP's sample, producing an IEnumerable of List<char>:
C#
public static class IEnumerableExtensions
{
    public static IEnumerable<List<T1>> GroupByContiguous<T1>(this IEnumerable<T1> src, T1 delimiter)
    {
        // trim any leading/trailing delimiters
        var src1 = src.TrimStart<T1>(delimiter)
            .TrimEnd<T1>(delimiter);

        List<T1> srcList = src1.ToList();

        T1 current;
        T1 previous = srcList.First();

        List<T1> element = new List<T1>();

        element.Add(previous);

        for (int i = 1; i < srcList.Count; i++)
        {
            current = srcList[i];

            if (current.Equals(delimiter))
            {
                previous = delimiter;
                continue;
            }

            if (! current.Equals(previous))
            {
                yield return element;
                element = new List<T1>();
                previous = current;
            }

            element.Add(current);
        }

        if (element.Count > 0) yield return element;
    }

    public static IEnumerable<T> TrimStart<T>(this IEnumerable<T> totrim, T trimthis)
    {
        return totrim.SkipWhile(itm => itm.Equals(trimthis));
    }

    public static IEnumerable<T> TrimEnd<T>(this IEnumerable<T> totrim, T trimthis)
    {
        return totrim.Reverse().SkipWhile(itm => itm.Equals(trimthis)).Reverse();
    }
}
Test with OP's data (with some extra delimiters thrown in for variety):
C#
private string text = "-DDDD-AAAA-DAAD-ADDDAA---";

// in some method:
var parsedChars = text.GroupByContiguous<char>(delimiter: '-').ToList();

foreach (var grp in parsedChars)
{
    Console.WriteLine(new string(grp.ToArray()));
}

// output to Console:
DDDD
AAAA
D
AA
D
A
DDD
AA
I've tested on basic ValueType and had no errors, but, an obvious issue when you start comparing complex objects (like instances of Classes) is: will using .Equals for the equality test suffice, or will you have to provide a custom implementation of IComparer.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP11-Sep-16 3:18
professional#realJSOP11-Sep-16 3:18 
GeneralRe: Group Sequance patterns with Linq Pin
BillWoodruff11-Sep-16 19:21
professionalBillWoodruff11-Sep-16 19:21 
Questionunable to bind json data in label Pin
Member 127294328-Sep-16 22:36
Member 127294328-Sep-16 22:36 
QuestionRe: unable to bind json data in label Pin
Richard MacCutchan8-Sep-16 22:40
mveRichard MacCutchan8-Sep-16 22:40 
AnswerRe: unable to bind json data in label Pin
Member 127294328-Sep-16 22:46
Member 127294328-Sep-16 22:46 
GeneralRe: unable to bind json data in label Pin
Richard MacCutchan8-Sep-16 22:53
mveRichard MacCutchan8-Sep-16 22:53 
AnswerRe: unable to bind json data in label Pin
Simon_Whale8-Sep-16 22:52
Simon_Whale8-Sep-16 22:52 
QuestionC# Website - PDF viewer to extract info Pin
NickyRamshaw8-Sep-16 3:12
NickyRamshaw8-Sep-16 3:12 
AnswerRe: C# Website - PDF viewer to extract info Pin
Nathan Minier8-Sep-16 5:49
professionalNathan Minier8-Sep-16 5:49 
QuestionRifidi connection Pin
Member 116390998-Sep-16 0:20
Member 116390998-Sep-16 0:20 
AnswerRe: Rifidi connection Pin
phil.o8-Sep-16 1:24
professionalphil.o8-Sep-16 1:24 
Generalhow to work on toggle button when two text fields in which we need one field require and save data and another second filed control toggle button. Pin
nadir malik7-Sep-16 21:45
nadir malik7-Sep-16 21:45 
GeneralRe: how to work on toggle button when two text fields in which we need one field require and save data and another second filed control toggle button. Pin
OriginalGriff7-Sep-16 21:58
mveOriginalGriff7-Sep-16 21:58 
Questionnoob need some help Pin
jimboo12327-Sep-16 7:31
jimboo12327-Sep-16 7:31 
AnswerRe: noob need some help Pin
OriginalGriff7-Sep-16 8:06
mveOriginalGriff7-Sep-16 8:06 
QuestionEntity Framework: How to migrate a change for specific table Pin
Tridip Bhattacharjee7-Sep-16 2:51
professionalTridip Bhattacharjee7-Sep-16 2:51 
AnswerRe: Entity Framework: How to migrate a change for specific table Pin
Dave Kreskowiak7-Sep-16 3:49
mveDave Kreskowiak7-Sep-16 3:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.