Eric Lippert's challenge “comma-quibbling”, best answer?

后端 未结 27 2187
日久生厌
日久生厌 2020-12-01 06:59

I wanted to bring this challenge to the attention of the stackoverflow community. The original problem and answers are here. BTW, if you did not follow it before, you should

27条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 07:05

    In .NET Core we can leverage SkipLast and TakeLast.

    public static string CommaQuibblify(IEnumerable items)
    {
        var head = string.Join(", ", items.SkipLast(2).Append(""));
        var tail = string.Join(" and ", items.TakeLast(2));
        return '{' + head + tail + '}';
    }
    

    https://dotnetfiddle.net/X58qvZ

提交回复
热议问题