|
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.
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?