Пример #1
0
        /// <summary>
        /// Render this collection of strings as a commalist (appends punctuation)
        /// </summary>
        /// <param name="stringList">The string list to join</param>
        /// <param name="mode">The punctuation mode</param>
        /// <returns>A commalist format string</returns>
        public static string CommaList(this string[] stringList, SplitListType mode)
        {
            if (stringList.Length == 0)
            {
                return(string.Empty);
            }

            if (stringList.Length == 1)
            {
                return(stringList[0]);
            }

            string returnString = string.Empty;

            switch (mode)
            {
            case SplitListType.AllAnd:
                returnString = string.Join(" and ", stringList);
                break;

            case SplitListType.AllComma:
                returnString = string.Join(", ", stringList);
                break;

            case SplitListType.CommaWithAnd:
                returnString = string.Join(", ", stringList);
                int lastComma = returnString.LastIndexOf(',');

                returnString = string.Format("{0} and {1}", returnString.Substring(0, lastComma), returnString.Substring(lastComma + 1));
                break;

            case SplitListType.OxfordComma:
                returnString = string.Join(", ", stringList);
                int lastOxfordComma = returnString.LastIndexOf(',');

                if (stringList.Count() == 2)
                {
                    returnString = string.Format("{0} and {1}", returnString.Substring(0, lastOxfordComma), returnString.Substring(lastOxfordComma + 1));
                }
                else
                {
                    returnString = string.Format("{0}, and {1}", returnString.Substring(0, lastOxfordComma), returnString.Substring(lastOxfordComma + 1));
                }
                break;
            }

            return(returnString);
        }
 public static string CommaList(this IEnumerable <string> stringList, SplitListType mode)
 {
     return(stringList.ToArray().CommaList(mode));
 }