Splice an array to String - CSharp System

CSharp examples for System:Array String

Description

Splice an array to String

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System;//from w w  w.ja  va 2  s . c om

public class Main{
        public static string Splice<T>(this IEnumerable<T> list, string quotes = "", string separator = ",")
        {
            var result = new StringBuilder();
            foreach (var each in list)
                result.AppendFormat("{0}{1}{0}{2}", quotes, each, separator);
            return result.ToString().TrimEnd(separator.ToCharArray());
        }
        public static string ToString<T>(this IEnumerable<T> collection, Func<T, string> exp, string separator = " ")
        {
            var sBuilder = new StringBuilder();
            foreach (var item in collection)
            {
                sBuilder.Append(exp(item));
                sBuilder.Append(separator);
            }
            return sBuilder.ToString(0, Math.Max(0, sBuilder.Length - separator.Length));
        }
        public static string ToString<T>(this IEnumerable<T> collection, string separator = " ")
        {
            return ToString(collection, t => t.ToString(), separator);
        }
}

Related Tutorials