9 Replies - 440 Views - Last Post: 14 April 2012 - 02:13 PM Rate Topic: -----

#1 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 11:18 AM

Hi

Instead of
List<string> allStrings = new List<string>(){"a","b,"c"};
string o = string.Empty;

foreach(string s in allStrings) o+=s+"\n";

mbox("strings: "+o);



i would like to do something like
mbox("strings: " + allStrings.ForEach(p=>p+"\n")) //of course this is wrong



is it possible?

Is This A Good Question/Topic? 0
  • +

Replies To: Short way to enumarate List<string> and return joined string

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1959
  • View blog
  • Posts: 8,700
  • Joined: 29-May 08

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 11:36 AM

It'll help you if you look at the methods available on IEnumerable, else well as their type-signatures.

You're after one that takes in an IEnumerable<T> and func and return a U (in your case that'll be a string).
The method will have the follow type-signature.
(IE<T>,(U,T)->U)->U


Spoiler

This post has been edited by AdamSpeight2008: 14 April 2012 - 11:36 AM

Was This Post Helpful? 2
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 916
  • View blog
  • Posts: 921
  • Joined: 30-September 10

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 11:39 AM

Hi,

This should give you the same output as the longer version:

Spoiler


Although do bear in mind that it is advisable to use the StringBuilder class for concatenating strings in a loop with any significant number of iterations (10+) to avoid unnecessary memory allocations.

EDIT: Sorry Adam, I didn't see your post there, and sort of gave the game away...

This post has been edited by CodingSup3rnatur@l-360: 14 April 2012 - 12:02 PM

Was This Post Helpful? 1
  • +
  • -

#4 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 12:07 PM

i searched for some examples and i don't quite understand what goes where.
allStrings.Appregate((result,item) => result += "-" + item + "\n")
//Output:
a-b
-c

allstrings.Appregate((i1, i2) => "-" + i1 + "\n" + i2)
//Output
--a
b
c

//And i need
-a
-b
-c


This post has been edited by Nerfpl: 14 April 2012 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 916
  • View blog
  • Posts: 921
  • Joined: 30-September 10

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 12:28 PM

To do that, you would need something like this:

Spoiler


Feel free to ask if you don't understand any part of that :)
Was This Post Helpful? 1
  • +
  • -

#6 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 12:35 PM

View PostCodingSup3rnatur@l-360, on 14 April 2012 - 12:28 PM, said:

To do that, you would need something like this:

Spoiler


Feel free to ask if you don't understand any part of that :)

Well it works but i still don't understand what's going on here, the logic and order. How it would look in raw/expanded form? Is 'Result' really a result and "item' really a current item?
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 12:54 PM

Basically enumerable.Aggregate(f, start) is the same as:

var result = start;
foreach(var x in enumerable) {
    result = f(result, x);
}
return result;



If you don't supply a starting value, the first element of the enumerable is used as the starting value (and the iteration starts with the second element). So that's how Aggregate works.

However for this case, I'd just use string.Join:

mbox("strings: " + string.Join("\n", allStrings) + "\n")



You could also use Select in conjunction with Join:

mbox("strings: " + string.Join("", allStrings.Select(s => s + "\n")));



Though in this case I'd prefer the former. Using Select would make more sense if you needed to perform a more complicated transformation on each element.

This post has been edited by sepp2k: 14 April 2012 - 02:11 PM

Was This Post Helpful? 2
  • +
  • -

#8 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 02:09 PM

in
string.Join("", allStrings.Where(s => s + "\n"))


i have 2 errors at 's + "\n"'
  • Error 2 Cannot implicitly convert type 'string' to 'bool'
  • Error 1 Cannot convert lambda expression to delegate type 'System.Func<string,bool>' because some of the return types in the block are not implicitly convertible to the delegate return type

Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 02:11 PM

Yeah, sorry, I was being stupid. That should have been Select, of course, not Where.
Was This Post Helpful? 1
  • +
  • -

#10 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Short way to enumarate List<string> and return joined string

Posted 14 April 2012 - 02:13 PM

string.Join("\n", duplicates.Select(p => "-"+p))

Works like charm :P

Thanks!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1