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

后端 未结 27 2200
日久生厌
日久生厌 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:13

    Late entry:

    public static string CommaQuibbling(IEnumerable items)
    {
        string[] parts = items.ToArray();
        StringBuilder result = new StringBuilder('{');
        for (int i = 0; i < parts.Length; i++)
        {
            if (i > 0)
                result.Append(i == parts.Length - 1 ? " and " : ", ");
            result.Append(parts[i]);
        }
        return result.Append('}').ToString();
    }
    

提交回复
热议问题