Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

Improving String.Format

Rate me:
Please Sign up or sign in to vote.
4.62/5 (17 votes)
17 Nov 2006CPOL6 min read 49.4K   298   37  
Creating an improved version of String.Format.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Niche.Utilities
{
    static public class StringUtils
    {

        static public string Format(string Template, Object Source)
        {
            // This regular expression should find named parameters of the form {id} or {id:parameter} 

            Regex r = new Regex(@"\{(?<id>\w+(\.\w+)*)(?<param>:[^}]+)?\}");

            MatchCollection mc = r.Matches(Template);
            StringBuilder result = new StringBuilder();

            // Loop over each match, replacing the placeholders with the result

            int index = 0;
            foreach (Match m in r.Matches(Template))
            {
                // Append content from before this match

                if (index < m.Index)
                    result.Append(Template.Substring(index, m.Index - index));

                // Get the details of this match
                Group id = m.Groups["id"];
                Group param = m.Groups["param"];

                // Create a template to pass to String.Format()
                string t = "{0" + param.ToString() + "}";

                // Get the property identified by id
                Object value = ObjectUtilities.ExtractPropertyValue(Source, id.ToString());

                // Format the value and add it to our result
                result.AppendFormat(t, value);

                index = m.Index + m.Length;
            }

            // Copy across any content from after the last match
            if (index < Template.Length)
            {
                result.Append(Template.Substring(index));
            }

            return result.ToString();

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
New Zealand New Zealand
A confirmed geek from an early age, Bevan's first computer was a ZX81 on which he learnt to program using Sinclair Basic. This was followed by a Spectrum 48K, the school's Apple //e network, an Amstrad CPC 6128, a whole lot of early Macintoshes and several PCs (though never a BEBox).

A former hardware service tech for Apple, Bevan is happiest when hip-deep in the code. Well, he actually prefers spending time with his family, but CodeProject is a techie site and who would believe that?

Comments and Discussions