Creates a string from a collection of items. - CSharp System

CSharp examples for System:String Convert

Description

Creates a string from a collection of items.

Demo Code

//   The MIT License (MIT)
using System.Text.RegularExpressions;
using System.Text;
using System.Collections;
using System;//from w w  w  .  j av a2s.  co m

public class Main{
        /// <summary>
        /// Creates a string from a collection of items.
        /// </summary>
        /// <param name="provider">
        /// The provider.
        /// </param>
        /// <param name="items">
        /// The items.
        /// </param>
        /// <param name="formatstring">
        /// The format string to apply to each item.
        /// </param>
        /// <param name="separator">
        /// The separator.
        /// </param>
        /// <returns>
        /// The collection as a string.
        /// </returns>
        public static object CreateList(
            IFormatProvider provider, IEnumerable items, string formatstring, string separator = ", ")
        {
            var sb = new StringBuilder();
            foreach (var item in items)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }

                sb.Append(string.Format(provider, formatstring, item));
            }

            return sb.ToString();
        }
        /// <summary>
        /// Replaces the format items in the specified string.
        /// </summary>
        /// <param name="provider">
        /// The culture specific format provider.
        /// </param>
        /// <param name="formatString">
        /// The format string.
        /// </param>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <param name="values">
        /// The values.
        /// </param>
        /// <remarks>
        /// The formatString and values works as in string.Format. In addition, you can format properties of the item object by using the syntax {PropertyName:Formatstring}. E.g. if you have a "Value" property in your item's class, use "{Value:0.00}" to output the value with two digits. Note that this formatting is using reflection and does not have the same performance as string.Format.
        /// </remarks>
        /// <returns>
        /// The formatted string.
        /// </returns>
        public static string Format(IFormatProvider provider, string formatString, object item, params object[] values)
        {
            // Replace items on the format {Property[:Formatstring]}
            var s = FormattingExpression.Replace(
                formatString,
                delegate(Match match)
                    {
                        var property = match.Groups["Property"].Value;
                        if (property.Length > 0 && char.IsDigit(property[0]))
                        {
                            return match.Value;
                        }

                        var pi = item.GetType().GetProperty(property);
                        if (pi == null)
                        {
                            return string.Empty;
                        }

                        var v = pi.GetValue(item, null);
                        var format = match.Groups["Format"].Value;

                        var fs = "{0" + format + "}";
                        return string.Format(provider, fs, v);
                    });

            // Also apply the standard formatting
            s = string.Format(provider, s, values);
            return s;
        }
}

Related Tutorials