Join the items together - CSharp System

CSharp examples for System:String Join

Description

Join the items together

Demo Code

/*//from  w  w w  . j  a  v  a  2  s.c  om
 * @version   : 2.5.0
 * @author    : Ext.NET, Inc. http://www.ext.net/
 * @date      : 2014-10-20
 * @copyright : Copyright (c) 2008-2014, Ext.NET, Inc. (http://www.ext.net/). All rights reserved.
 * @license   : See license.txt and http://www.ext.net/license/. 
 * @website   : http://www.ext.net/
 */
using System.Web.UI;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        /// <summary>
        /// Join the items together
        /// </summary>
        /// <param name="items">The items to join.</param>
        /// <param name="separator">The separator.</param>
        /// <param name="template">The template to format the items with.</param>
        /// <returns></returns>
        public static string Join(this IEnumerable items, string separator, string template)
        {
            StringBuilder sb = new StringBuilder();

            foreach (object item in items)
            {
                if (item != null)
                {
                    sb.Append(separator);
                    sb.Append(string.Format(template, item.ToString()));
                }
            }

            return sb.ToString().RightOf(separator);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public static string Join(this IEnumerable items, string separator)
        {
            return items.Join(separator, "{0}");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string Join(this IEnumerable items)
        {
            return items.Join(",", "{0}");
        }
        /// <summary>
        /// Right of the n'th occurrence of c
        /// </summary>
        public static string RightOf(this string text, string c, int n)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            int i = -1;
            
            while (n != 0)
            {
                i = text.IndexOf(c, i + 1);
                if (i == -1)
                {
                    return "";
                }
                --n;
            }
            
            return text.Substring(i + 1);
        }
        /// <summary>
        /// Right of the n'th occurrence of c
        /// </summary>
        public static string RightOf(this string text, char c, int n)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            int i = -1;
            
            while (n != 0)
            {
                i = text.IndexOf(c, i + 1);
                if (i == -1)
                {
                    return "";
                }
                --n;
            }
            
            return text.Substring(i + 1);
        }
        /// <summary>
        /// Right of the first occurrence of text
        /// </summary>
        public static string RightOf(this string text, string value)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            int i = text.IndexOf(value);
            
            if (i == -1)
            {
                return "";
            }
            
            return text.Substring(i + value.Length);
        }
        /// <summary>
        /// Right of the first occurrence of c
        /// </summary>
        public static string RightOf(this string text, char c)
        {
            if (text.IsEmpty())
            {
                return text;
            }

            int i = text.IndexOf(c);
            
            if (i == -1)
            {
                return "";
            }
            
            return text.Substring(i + 1);
        }
}

Related Tutorials