开发者

How do I use the Aggregate function to take a list of strings and output a single string separated by a space?

How do I use the Aggregate function to take a list of strings and output a single string separated by a space?

Here is the source code for this test:

    var tags = new List<string> {"Portland", "Code","StackExcahnge" };
    const string separator = " ";
    tagString = tags.Aggregate(t => , separator);
    Console.WriteLine(tagString);
    // Ex开发者_StackOverflow社区pecting to see "Portland Code StackExchange"
    Console.ReadKey();

Update

Here is the solution I am now using:

var tagString = string.Join(separator, tags.ToArray());

Turns out string.Join does what I need.


For that you can just use string.Join.


string result = tags.Aggregate((acc, s) => acc + separator + s);

or simply

string result = string.Join(separator, tags);


String.Join Method may be?


This is what I use

public static string Join(this IEnumerable<string> strings, string seperator)
{
    return string.Join(seperator, strings.ToArray());
}

And then it looks like this

tagString = tags.Join(" ")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜