string 帮助类 list2string seperator
string 帮助类 list2string seperator
public static class StringExtensions
    {
        public static int IndexOfTimes(this string s, string value, int times)
        {
            return s.IndexOfTimes(value, 0, times);
        }
        public static int IndexOfTimes(this string s, string value, int startindex, int times)
        {
            if (string.IsNullOrEmpty(s)) return -1;
            startindex = startindex < 0 ? 0 : startindex;
            if (times <= 0)
                return -1;
            int at = 0;
            int count = 0;
            while (startindex < s.Length)
            {
                at = s.IndexOf(value, startindex);
                if (at == -1)
                    return -1;
                count++;
                if (count == times)
                    return at;
                startindex = at + 1;
            }
            return -1;
        }
        public static string ReplaceFirst(this string s, string oldValue, string newValue)
        {
            return s.ReplaceTime(oldValue, newValue, 1);
        }
        public static string ReplaceTime(this string s, string oldValue, string newValue, int times)
        {
            int pos = s.IndexOfTimes(oldValue, times);
            if (pos < 0)
                return s;
            if (string.IsNullOrEmpty(newValue))
                return "";
            s = s.Remove(pos, oldValue.Length);
            s = s.Insert(pos, newValue);
            return s;
        }
        // for generic interface IEnumerable<T>     
        public static string ToString<T>(this IEnumerable<T> source, string separator)
        {
            if (source == null)
                throw new ArgumentException("Parameter source can not be null.");
            if (string.IsNullOrEmpty(separator))
                throw new ArgumentException("Parameter separator can not be null or empty.");
            string[] array = source.Where(n => n != null).Select(n => n.ToString()).ToArray();
            return string.Join(separator, array);
        }     // for interface IEnumerable     
        public static string ToString(this IEnumerable source, string separator)
        {
            if (source == null)
                throw new ArgumentException("Parameter source can not be null.");
            if (string.IsNullOrEmpty(separator))
                throw new ArgumentException("Parameter separator can not be null or empty.");
            string[] array = source.Cast<object>().Where(n => n != null).Select(n => n.ToString()).ToArray();
            return string.Join(separator, array);
        }
        /// <summary>
        /// Converts List to string with given separator.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <param name="separator">The separator.</param>
        /// <returns></returns>
        public static string ToString(this List<string> list, string separator)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in list)
            {
                sb.Append(string.Format("{0}{1}", s, separator));
            }
            string returnString = string.Empty;
            //Remove the last separator from the list
            if (sb.Length > 0)
            {
                returnString = sb.Remove(sb.ToString().LastIndexOf(separator), separator.Length).ToString();
            }
            return returnString;
        }
        /// <summary>
        /// Strings to string list.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="separator">The separator.</param>
        /// <returns></returns>
        public static List<string> StringList(this string items, char separator)
        {
            List<string> list = new List<string>();
            string[] listItmes = items.Split(separator);
            foreach (string item in listItmes)
            {
                list.Add(item);
            }
            if (list.Count > 0)
                return list;
            else
                return null;
        }
    }
 
                    
                     
                    
                 
                    
                 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号