Join ICollection As String - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Join ICollection As String

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;// w w  w  . ja  v  a  2  s  .c  om

public class Main{
    public static string JoinAsString<T>(this ICollection<T> col, string propName, string sep)
      {
         List<String> strs = new List<String>();

         foreach (T obj in col)
            strs.Add(obj.GetPropValue(propName).ToString());

         return JoinAsString(strs, sep);
      }
    public static string JoinAsString(this ICollection<string> strList, string sep)
      {
         if (sep == null)
            sep = "";

         StringBuilder sb = new StringBuilder();
         foreach (string str in strList)
         {
            sb.AppendFormat("{0}{1}", str, sep);
         }

         if (sb.Length > 2)
            return sb.ToString().Substring(0, sb.Length - sep.Length);
         else
            return null;
      }
}

Related Tutorials