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

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

    public static string CommaQuibbling(IEnumerable items)
    {
       var itemArray = items.ToArray();
    
       var commaSeparated = String.Join(", ", itemArray, 0, Math.Max(itemArray.Length - 1, 0));
       if (commaSeparated.Length > 0) commaSeparated += " and ";
    
       return "{" + commaSeparated + itemArray.LastOrDefault() + "}";
    }
    

提交回复
热议问题