tags:

views:

225

answers:

5
string x;
foreach(var item in collection)
{
   x += item+",";
}

can I write something like this with lambdas?

+1  A: 

Does this answer your question?

http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c

Note that you can do this with Aggregate, but the built-in string.Join is both easier to use and faster for long arrays.

Daniel Earwicker
+1  A: 
string[] text = new string[] { "asd", "123", "zxc", "456" };

var result = texts.Aggregate((total, str) => total + "," + str);

Shorter syntax, thanks to Earwicker

Kamarey
Much simpler: texts.Aggregate((a, b) => a + "," + b) - no need to trim the end off afterwards.
Daniel Earwicker
Thanks, you right. Strange, but got the ',' at the end 5 min ago...
Kamarey
what's the easiest way to test out a command like this? do i have to create a new c# project every time
you can keep some "test" project for small tests like this... or create a new one - doesn't take much time. Also you may try the LinqPad, but not sure about it
Kamarey
@decon - I keep a C# Console Mode project called Scratch on my computer, ready for me to try things out, just keep reusing it.
Daniel Earwicker
I hate to say it, but while it might be *interesting* way of doing it (and is FP, etc), it isn't really a good answer to the problem... so many ineffiiencies compared to using the pre-rolled string.Join, or a bespoke Join over an IEnumerable<string> using a StringBuilder.
Marc Gravell
+18  A: 

Assuming C#, have you tried String.Join()? Or is using lambdas mandatory?

Example:

string[] myStrings = ....;
string result = String.Join(",", myStrings);

EDIT

Although the original title (and example) was about concatenating strings with a separator (to which String.Join() does the best job in my opinion), the original poster seems to ask about the broader solution: how to apply a custom format a list of strings.

My answer to that is write your own method. String.Join has a purpose, reflected by its name (joins some strings). It's high chance that your format logic has a meaning in your project, so write it, give it a proper name and use it.

For instance, if you want to output <li>text</li> for every item, make something as this:

string FormatAsListItems(string[] myStrings)
{
    StringBuilder sb = new StringBuilder();
    foreach (string myString in myStrings)
    {
         sb.Append("<li>").Append(myString).Append("</li>");
    }
}

I think the intent is clearer, and you also don't take the performance hit of concatenating strings in a loop.

Dan C.
no how do you use Join?
See added example.
Dan C.
Much tidier than wrestling with Lambdas in my opinion
codeulike
You were robbed - never mind, the 12 upvotes should help...
Marc Gravell
The original question is: can I write something like this with lambdas? This isn't closest answer. I also would use String.Join in simple strings concatenation. But adding some complex logic makes this function useless.
Kamarey
@Kamarey: While the OP was talking of loops and lambdas, this is the correct answer to the problem of converting an array of strings into a string string with separators.
Richard
@Kamarey: the original question was first "convert a string[] to string with out using foreach". If he's going to use lambdas for that... well, it's his code, in his project :).And if you're adding some extra logic, I still wouldn't use lambdas for that; make it a separate method then.
Dan C.
well, its still good to have this answer here for other people who are pondering similar questions
codeulike
i like your solution. but it wouldn't work if i wanted to enclose each element with something like <li>string</li>i should have made it more clear in my post
@decon: If you wanted that, you'd better make it a separate method and use a StringBuilder to format the results in a loop. I still wouldn't use a lambda for that.
Dan C.
+5  A: 
string x = string.Join(",", collection);
Marc Gravell
+2  A: 

You are looking too far for the solution. The String class has a Join method for this:

string x = String.Join(",", collection);
Guffa