Implode ICollection to String - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Implode ICollection to String

Demo Code


using System.Collections.Specialized;
using System.Collections;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/* w ww.j av a  2  s  .  c  om*/

public class Main{

        public static string Implode(ICollection collection, string separator)
        {
            if (collection == null || collection.Count == 0) return string.Empty;
            if (separator == null) separator = string.Empty;
            
            StringBuilder sb = new StringBuilder();
            foreach (object item in collection)
            {
                sb.Append(item.ToString() + separator);
            }

            string result = sb.ToString().TrimEnd(separator.ToCharArray());
            return result;
        }
}

Related Tutorials