本文介绍了加入列表< string>与Commas Plus"and"一起使用最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以找到一条出路,但我想知道是否有更简洁的解决方案.总是有String.Join(", ", lList)lList.Aggregate((a, b) => a + ", " + b);,但是我想为最后一个添加例外,以", and "作为其连接字符串. Aggregate()是否可以在某些地方使用某些索引值?谢谢.

I know I could figure a way out but I am wondering if there is a more concise solution. There's always String.Join(", ", lList) and lList.Aggregate((a, b) => a + ", " + b); but I want to add an exception for the last one to have ", and " as its joining string. Does Aggregate() have some index value somewhere I can use? Thanks.

推荐答案

您可以这样做

string finalString = String.Join(", ", myList.ToArray(), 0, myList.Count - 1) + ", and " + myList.LastOrDefault();

这篇关于加入列表< string>与Commas Plus"and"一起使用最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 07:07