Blog Archives

String Formatting

Snippets below have been condensed from their original sources for brevity. See references for original articles.

All examples are in C# .Net.

My own little function:

   private string DynamicFormat(string dbformatstring, string contentstring)
        {
            foreach (string item in dbformatstring.Split(',')) {
            string propname = item.Split(':')[0];
                string propvalue = item.Split(':')[1];
                contentstring = contentstring.Replace("{"+propname+"}",propvalue);
            }

            return contentstring;
        }

Format With extension method by James Newton-King:

public static string FormatWith(this string format, params object[] args)
{
  if (format == null)
    throw new ArgumentNullException("format");
 
  return string.Format(format, args);
}
 
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
  if (format == null)
    throw new ArgumentNullException("format");
 
  return string.Format(provider, format, args);
}

References
James Newton-King, “FormatWith”,http://james.newtonking.com/archive/2008/03/27/formatwith-string-format-extension-method.aspx

Advertisement