none
aggregate on a string RRS feed

  • Question

  • string s = "1a2b34cd5"; I want to get "abcd" by using Aggregate function something like this:

    s.ToCharArray().Aggregate((current, next) => ..... ).Dump();

    I have so many ways to do this such as:

    Regex.Replace(s,@"\d","").Dump();

    string.Join( "",

    (

    from c in s.ToCharArray()

    where Char.IsLetter( c )

    select c.ToString()

    )).Dump();

    Just want to see if someone can use Aggregate function to solve it. thanks!

    Thursday, May 27, 2010 6:11 AM

Answers

  • My life is much reply on intellisense...


    If you want to see the "Aggregate" method show up in IntelliSense you can still use ToCharArray.

     

    Change this:

    string result = input.Aggregate("", (s, c) => s + (Char.IsLetter(c) ? c.ToString() : ""));

    To this:

    string result = input.ToCharArray().Aggregate("", (s, c) => s + (Char.IsLetter(c) ? c.ToString() : ""));

     

    That way you will see "Aggregate" show up as soon as you press "." after "ToCharArray()". I was merely pointing out that it is redundant. IntelliSense still works and gives you parameter info and suggestions after using "Aggregate".


    Document my code? Why do you think it's called "code"?
    • Marked as answer by Meidi Thursday, May 27, 2010 9:38 PM
    Thursday, May 27, 2010 8:27 PM

All replies

  • A quick try is:

    s
    	.ToCharArray()
    	.Select(c=>c.ToString())
    	.Aggregate((current, next) => 
    		(Char.IsLetter(current[0])?current:"") 
    		+ (Char.IsLetter(next[0])?next:"") 
    	).Dump();

     

    Sorry to have a lot of string <--> char conversions, but char does not support concatenation and string does not have an IsLetter method.


    Regards,


    Syed Mehroz Alam
    My Blog | My Articles
    Thursday, May 27, 2010 1:16 PM
  • yes, that's my dilemma too.
    Thursday, May 27, 2010 2:32 PM
  • Here's an approach:


    string input = "1a2b34cd5";
    string result = input.Aggregate("", (s, c) => s + (Char.IsLetter(c) ? c.ToString() : ""));
    Console.WriteLine(result);

     

    Strings are essentially IEnumerable<char> so you can use LINQ extension methods on them directly rather than using .ToCharArray() first. It doesn't show up in IntelliSense but it does work.


    Document my code? Why do you think it's called "code"?
    Thursday, May 27, 2010 7:12 PM
  • My life is much reply on intellisense...
    Thursday, May 27, 2010 7:55 PM
  • My life is much reply on intellisense...


    If you want to see the "Aggregate" method show up in IntelliSense you can still use ToCharArray.

     

    Change this:

    string result = input.Aggregate("", (s, c) => s + (Char.IsLetter(c) ? c.ToString() : ""));

    To this:

    string result = input.ToCharArray().Aggregate("", (s, c) => s + (Char.IsLetter(c) ? c.ToString() : ""));

     

    That way you will see "Aggregate" show up as soon as you press "." after "ToCharArray()". I was merely pointing out that it is redundant. IntelliSense still works and gives you parameter info and suggestions after using "Aggregate".


    Document my code? Why do you think it's called "code"?
    • Marked as answer by Meidi Thursday, May 27, 2010 9:38 PM
    Thursday, May 27, 2010 8:27 PM