Contacts the items with delim. - CSharp System

CSharp examples for System:String Format

Description

Contacts the items with delim.

Demo Code


using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using System;// w  w  w .j  a  va  2 s.co  m

public class Main{
        /// <summary>
        /// Contacts the items with delim.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items">The items.</param>
        /// <param name="delim">The delim.</param>
        /// <param name="initialValue">The initial value.</param>
        /// <param name="endValue">The end value.</param>
        /// <returns></returns>
        public static string ContactWithDelim<T>(IEnumerable<T> items, string delim, string initialValue, string endValue)
        {
            StringBuilder text = new StringBuilder();
            text.Append(initialValue);
            bool isFirst = true;
            foreach (T item in items)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    text.Append(delim);
                }
                text.Append(item.ToString());
            }
            text.Append(endValue);
            return text.ToString();
        }
        /// <summary>
        /// Contacts the items with delim.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items">The items.</param>
        /// <param name="delim">The delim.</param>
        /// <returns></returns>
        public static string ContactWithDelim<T>(IEnumerable<T> items, string delim)
        {
            return ContactWithDelim<T>(items, delim, null, null);
        }
        /// <summary>
        /// Contacts the items with delim.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="delim">The delim.</param>
        /// <param name="initialValue">The initial value.</param>
        /// <param name="endValue">The end value.</param>
        /// <returns></returns>
        public static string ContactWithDelim(IEnumerable<string> items, string delim, string initialValue, string endValue)
        {
            StringBuilder text = new StringBuilder(initialValue);
            bool isFirst = true;
            foreach (string item in items)
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    text.Append(delim);
                }
                text.Append(item);
            }
            text.Append(endValue);
            return text.ToString();
        }
        /// <summary>
        /// Contacts the items with delim.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="delim">The delim.</param>
        /// <param name="initialValue">The initial value.</param>
        /// <returns></returns>
        public static string ContactWithDelim(IEnumerable<string> items, string delim, string initialValue)
        {
            return ContactWithDelim(items, delim, initialValue, null);
        }
        /// <summary>
        /// Contacts the items with delim.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="delim">The delim.</param>
        /// <returns></returns>
        public static string ContactWithDelim(IEnumerable<string> items, string delim)
        {
            return ContactWithDelim(items, delim, null, null);
        }
        /// <summary>
        /// Contacts the with delim.
        /// </summary>
        /// <param name="str1">The STR1.</param>
        /// <param name="delim">The delim.</param>
        /// <param name="str2">The STR2.</param>
        /// <returns></returns>
        public static string ContactWithDelim(string str1, string delim, string str2)
        {
            if (string.IsNullOrEmpty(str1)) return str2;
            else if (string.IsNullOrEmpty(str2)) return str1;
            else return str1 + delim + str2;
        }
}

Related Tutorials