StringHelper.NamedFormat is a String.Format alternative which will let you use names instead of numbers in your string templates.
As you know C# provides similar functionality to printf using String.Format static function. System.Format lets you fill a string template with ordered data. This is mostly enough for the developers, if you have the templates hard coded into your source code. However, if you are planing to keep those templates in a text file which could be modified by users, such as for translation purposes etc., then those secret numbers {0} and {1} won't make much sense for them at all, and it will make harder for users to modify the templates.
Using StringHelper.NamedFormat, you can use names instead of numbers in your templates. For example, here is a regular String.Format template:
const string template = "You can download {0} from {1}.";
You can write the same template for NamedFormat by replacing numbers with names:
const string template = "You can download {library_name} from {web_address}.";
As you can see it is much easier to understand the variables now.
But, the usage is a little different than String.Format. NamedFormat relies on delegates to communicate with your application when it needs to look up the value of a variable.
As an example, I will try to convert this String.Format based code to NamedFormat:
const string template = "You can download {0} from {1}.";
const string libraryName = "ZDO.Commons";
const string webAddress = "www.zdo.com";
string text = String.Format(template, libraryName, webAddress);
The NamedFormat equivalent for this code is:
const string template = "You can download {library_name} from {web_address}.";
const string libraryName = "ZDO.Commons";
const string webAddress = "www.zdo.com";
string text = StringHelper.NamedFormat(template, delegate(string name)
{
if ("library_name".Equals(name))
{
return libraryName;
}
else if ("web_address".Equals(name))
{
return webAddress;
}
return String.Empty;
});
The code assumes that your variable names are market by { and } characters, however, if you are using different characters for that purpose, then you can call the following function instead:
StringHelper.NamedFormat('left character', 'right character',
'escape character', template, handler);
Replace the left, right, and escape characters with the ones you are planing to use in your template.
Currently NamedFormat only places the values of your variables into your template, it does not support any number formatting functionality like String.Format provides. Buy you should be able to extend it very easily to provide that functionality as well. If you extend it in any way, please send me your version so that I can update this page.
/*
* StringHelper.
*
* Copyright (c) 2007
* A. Onur Cinar &060;cinar(a)zdo.com&062;
*
* This program is provided under LGPL license agreement.
*
*/
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace ZDO.Commons.Helpers
{
public sealed class StringHelper
{
#region Delegates
public delegate string NamedFormatHandler (string name);
#endregion
#region Format helpers
public static string NamedFormat (string format, NamedFormatHandler handler)
{
return NamedFormat('{', '}', '\\', format, handler);
}
public static string NamedFormat (char nameStartChar, char nameEndChar,
char escapeChar, string format, NamedFormatHandler handler)
{
StringBuilder builder = new StringBuilder();
bool escape = false;
int nameStart = -1;
for (int i = 0; i < format.Length; i++)
{
if (!escape)
{
if (nameStartChar.Equals(format[i]))
{
nameStart = i + 1;
}
else if (nameEndChar.Equals(format[i]))
{
builder.Append(handler(format.Substring(
nameStart, i - nameStart)));
nameStart = -1;
}
else if (escapeChar.Equals(format[i]))
{
escape = true;
}
else if (nameStart == -1)
{
builder.Append(format[i]);
}
}
else
{
escape = false;
builder.Append(format[i]);
}
}
return builder.ToString();
}
#endregion
}
}
If you found this post helpful, please "Kick" it so others can find it too:
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Tracked: Nov 21, 04:01