开发者

Append 'List' items to StringBuilder

开发者 https://www.devze.com 2023-01-26 12:45 出处:网络
I tried to append the items in a List<string> to a StringBuilder with LINQ: items.Select(i => sb.Append(i + \",\"));

I tried to append the items in a List<string> to a StringBuilder with LINQ:

items.Select(i => sb.Append(i + ","));

I fou开发者_运维百科nd a similar question here which explains why the above doesn't work, but I couldn't find an Each of ForEach or anything similar on List which I could use instead.

Is there a neat way of doing this in a one-liner?


items.ForEach(item => sb.Append(item + ","));


You could use a simple foreach loop. That way you have statements which modify the StringBuilder, instead of using an expression with side-effects.

And perhaps your problem is better solved with String.Join(",", items).


Try this:

 String [] items={"Jan","Feb","Mar"};
 StringBuilder sb=new StringBuilder();
 foreach(var entity in items)
 {
   sb.Append(entity + Environment.NewLine);
 }
 Textbox1.Text=sb.tostring();

Output:

Jan

Feb

Mar

0

精彩评论

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

关注公众号