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

Advertisements

About Ronnie Diaz

Ronnie Diaz is an enterprise software engineer responsible for front-end and back-end development for companies in many industries. Heavily involved in cloud development, online retail, e-commerce and electronic ordering, fulfillment and customer relational systems.

Posted on February 1, 2011, in Programming & Development and tagged , , , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: