Join IEnumerable To String by Format - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Join IEnumerable To String by Format

Demo Code


using System.Linq;
using System.Text;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;

public class Main{
        public static string JoinToStringFormat<T>(this IEnumerable<T> list, string delimiter, IFormatProvider info)
                where T: IFormattable
            {//from  w w w  .  ja v a 2 s  .c  o  m
    
                var sb = new StringBuilder();

                bool primero = true;

                foreach (T x in list)
                {
                    if (primero)
                    {
                        primero = false;
                    }
                    else
                    {
                        sb.Append(delimiter);
                    }
                    sb.Append(x.ToString(null, info));
                }

                return sb.ToString();
            }
        public static string JoinToStringFormat<T>(this IEnumerable<T> list, string delimiter)
            where T : IFormattable
        {
            return list.JoinToStringFormat(delimiter, null);
        }
        public static string JoinToStringFormat<T>(this IEnumerable<T> list)
            where T : IFormattable
        {
            return list.JoinToStringFormat(",");
        }
}

Related Tutorials