Joins the element in the collection using fn to convert each element to string - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Joins the element in the collection using fn to convert each element to string

Demo Code


using System.Linq;
using System.Text;
using System.Collections.Generic;
using System;//from  w  w w.  ja va2s  .  c  om

public class Main{
        /// <summary>
        /// Joins the element in the collection using fn to convert each element to string
        /// </summary>
        /// <typeparam name="T">Type of the collection</typeparam>
        /// <param name="collection">Collection to use</param>
        /// <param name="separator">Separator to use</param>
        /// <param name="fn">Function to use to convert to string</param>
        /// <returns>A string with the result of fn(x) on each element separated by separator</returns>
        public static string Join<T>(this IEnumerable<T> collection, char separator, Func<T, string> fn)
        {
            return collection
                .Head()
                .Aggregate(new StringBuilder(), (b, x) =>
                                                    {
                                                        b.Append(fn(x));
                                                        b.Append(separator);
                                                        return b;
                                                    })
                .Append(collection.Last())
                .ToString();
        }
        /// <summary>
        /// Joins the elements of a collection to a string using a separator
        /// </summary>
        /// <typeparam name="T">Type of the collection</typeparam>
        /// <param name="collection">Collection to use</param>
        /// <param name="separator">Separator to use</param>
        /// <returns>A string with all the elements string representation separated by separator</returns>
        public static string Join<T>(this IEnumerable<T> collection, char separator)
        {
            Func<T, string> asStringCheckingNull = x => Equals(x, null) ? "null" : x.ToString();

            return collection.Join(separator, asStringCheckingNull);
        }
}

Related Tutorials