List of strings to one string

前端 未结 6 692
清酒与你
清酒与你 2020-12-22 22:29

Lets say you have a:

List los = new List();

In this crazy functional world we live in these days which one of t

相关标签:
6条回答
  • 2020-12-22 22:57
    string.Concat(los.ToArray());
    

    If you just want to concatenate the strings then use string.Concat() instead of string.Join().

    0 讨论(0)
  • 2020-12-22 22:58

    My vote is string.Join

    No need for lambda evaluations and temporary functions to be created, fewer function calls, less stack pushing and popping.

    0 讨论(0)
  • 2020-12-22 23:08

    If you use .net 4.0 you can use a sorter way:

    String.Join<string>(String.Empty, los);
    
    0 讨论(0)
  • 2020-12-22 23:09
    los.Aggregate((current, next) => current + "," + next);
    
    0 讨论(0)
  • 2020-12-22 23:10

    String.Join() is implemented quite fast, and as you already have a collection of the strings in question, is probably the best choice. Above all, it shouts "I'm joining a list of strings!" Always nice.

    0 讨论(0)
  • 2020-12-22 23:23

    I would go with option A:

    String.Join(String.Empty, los.ToArray());
    

    My reasoning is because the Join method was written for that purpose. In fact if you look at Reflector, you'll see that unsafe code was used to really optimize it. The other two also WORK, but I think the Join function was written for this purpose, and I would guess, the most efficient. I could be wrong though...

    As per @Nuri YILMAZ without .ToArray(), but this is .NET 4+:

    String.Join(String.Empty, los);
    
    0 讨论(0)
提交回复
热议问题