Conditional formatting of LINQ query RRS feed

  • Question

  • Hi all, I have a string of characters and I'm trying to set up a query that'll substitute a specific sequence of similar characters into a character count. Here's an example of what I'm trying to do:


    In this case, I'm trying to isolate and count ONLY the occurrences of the number 5, so my output should read:


    My current code is the following, where GroupAdjacentBy is a function that groups and counts the character occurrences as above.

                            var res = text
                                  .GroupAdjacentBy((l, r) => l == r)
                                  .Select(x => new { n = x.First(), c = x.Count()})

    The problem is that the above function groups and counts EVERY SINGLE character in my string, not the just the one character I'm after. Is there a way to conditionally perform that operation on ONLY the character I need counted?

    Tuesday, December 3, 2019 9:57 AM

All replies

  • If you are also interested in some alternatives, then check this:

    string text = "agd69dnbd555bdggjykbcx555555bbb5x";

    string result = Regex.Replace( text, "5{2,}", m => m.Length.ToString( ) );


    In order to replace isolated “5” with “1”, use a different pattern: “5+”.

    Or show details about GroupAdjacentBy.

    Tuesday, December 3, 2019 11:37 AM
  • Fantastic! Thank you so much.

    What if I were doing the same, but with arbitrary patterns? I'll illustrate my question with an example: agd69dnbd555bdggjykbcx555555bbb. The query would substitute the similar patterns it finds with a string of my choice, ex. "OK". In this case, it would substitute all occurrences of "bd" and "555" with "OK", resulting in agd69dnOKOKOKggjykbcxOKOKbbb

    Tuesday, December 3, 2019 11:55 AM
  • Try this:

    string text = "agd69dnbd555bdggjykbcx555555bbb";

    string result = Regex.Replace( text, "bd|5+", "OK" );

    Tuesday, December 3, 2019 12:03 PM
  • The patterns are arbitrary. The routine should recognize them by itself.

    Right now, I'm using this code:

    var tmp = lines 
    .GroupBy(g => g) 
    .Where(x => x.Count() > 1 && x.Key.Length > 1) 
    .Select(s => new { Key = s.Key, Count = s.Count()})
    .ToDictionary(d => d.Key, d => d.Count); 

    But I have to iterate through every resulting item in order to replace the repetitions. It's quite machine heavy if the original text is very long. I saw that LINQ statements are extremely fast. Can I adapt it to do what I need?

    • Edited by Robert1967 Tuesday, December 3, 2019 12:29 PM
    Tuesday, December 3, 2019 12:24 PM
  • Hi Robert1967,
    >> I saw that LINQ statements are extremely fast
    After using LINQ statement, you also replace the number in the string with the count of number by using the method Regex.Replace.
    So in my opinion, LINQ statement is not faster.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, December 4, 2019 8:29 AM