예제 #1
0
        // These extensions allow a list of strings to be joined together

        /// <summary>
        /// Joins all strings together into a single string, separating each item with the separator.
        /// <para>For example: </para> <c>alphabet.JoinStrings(", ") == "a, b, ... y, z"</c>
        /// </summary>
        /// <param name="source">The source of strings to join together</param>
        /// <param name="separator">The text to insert between items</param>
        public static string JoinStrings(this IEnumerable <string> source, string separator)
        {
            ArgumentValidator.CheckForNullReference(source, "source");
            return(string.Join(separator, source as string[] ?? source.ToArray()));
        }
예제 #2
0
 /// <summary>Splits the enumeration into segments,
 /// each segment containing the portion between predicate matches.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="splitPredicate">The condition that will create a new split.
 /// The matching item will be the last element in the previous split.
 /// This predicate incorporates the element's index.
 /// </param>
 public static IEnumerable <TSource[]> SplitAfter <TSource>(this IEnumerable <TSource> source, Func <TSource, int, bool> splitPredicate)
 {
     ArgumentValidator.CheckForNullReference(source, "source");
     ArgumentValidator.CheckForNullReference(splitPredicate, "splitPredicate");
     return(SplitIterator(source, null, splitPredicate, true));
 }
예제 #3
0
 /// <summary>Uses String.Format to format each item in the collection using the specified format.</summary>
 /// <param name="source"></param>
 /// <param name="format">
 /// A composite format string; same as String.Format.
 /// {0} refers to the source object, {1} refers to the index.
 /// </param>
 public static IEnumerable <string> FormatEach <T>(this IEnumerable <T> source, string format)
 {
     ArgumentValidator.CheckForNullReference(source, "source");
     ArgumentValidator.CheckForNullReference(format, "format");
     return(source.Select((arg, index) => string.Format(format, new object[] { arg, index })));
 }
예제 #4
0
 /// <summary>Splits the enumeration into segments,
 /// each segment containing [count] items, and the last segment containing the remainder.
 /// </summary>
 /// <param name="source"></param>
 /// <param name="count">The number of items to include in each segment.
 /// The last segment might contain fewer items.</param>
 public static IEnumerable <TSource[]> Split <TSource>(this IEnumerable <TSource> source, int count)
 {
     ArgumentValidator.CheckForNullReference(source, "source");
     ArgumentValidator.CheckForZeroValue(count, "count");
     return(SplitIterator(source, count));
 }
예제 #5
0
 /// <summary>Chooses one of the items at random.
 ///
 /// Returns default if there are no items.
 /// </summary>
 public static T RandomOrDefault <T>(this IEnumerable <T> source)
 {
     ArgumentValidator.CheckForNullReference(source, "source");
     return(RandomIterator(source));
 }
예제 #6
0
 /// <summary>Chooses one of the items at random.
 ///
 /// Throws an exception if there are no items.
 /// </summary>
 public static T Random <T>(this IEnumerable <T> source)
 {
     ArgumentValidator.CheckForEmpty(source, "source");
     return(RandomIterator(source));
 }