Introduction
System.String
type gives us a very useful '
Format
' method, that replaces one or more format items (keys) in a specified string with the concrete values. The method determines necessary value by its position in arguments set.
So, if the format string is short or number of format items is few, we can easily control the correspondence between keys in '
format
' string and values in arguments set. But if the string is too large and number of arguments is great, we can be confused in arguments order.
Body
Two methods that use names as a keys in '
format
' string are presented here. If we use names as keys, and Name-Value pairs as arguments, the confusion probability is reduced.
First, we define
NamedValue
class that contains a name and a value of the argument.
public class NamedValue {
private string _name = "";
public string Name {
get { return _name; }
set {
if (value == null || value.Trim() == "") {
throw new ArgumentException();
}
_name = value.Trim();
}
}
private object _value = null;
public object Value {
get { return _value; }
set { _value = value; }
}
public NamedValue() { }
public NamedValue(string name, object value) {
Name = name;
Value = value;
}
}
Next
static
class
StringFormat
contains methods,
CreateSimpleNamedFormat
and
CreateNamedFormat
. First argument of each method is format string, next is a set of arguments of
NamedValue
class, almost like in native '
Format
'. The functions replace all occurrences of embraced keys in '
format
' string by its values. It gets value from the property
Value
of correspondence
NamedValue
instance in arguments set.
public static class StringFormat {
public static string CreateSimpleNamedFormat(string format, params NamedValue[] args) {
for (int i = 0; i < args.Length; i++) {
format = format.Replace("{" + args[i].Name + "}", args[i].Value.ToString());
}
return format;
}
public static string CreateNamedFormat(string format, params NamedValue[] args) {
List<object> argsValues = new List<object>();
for (int i = 0; i < args.Length; i++) {
NamedValue curArg = args[i];
format = format.Replace("{" + curArg.Name + "}", "{" + i.ToString() + "}");
format = format.Replace("{" + curArg.Name + ":", "{" + i.ToString() + ":");
argsValues.Add(curArg.Value);
}
return string.Format(format, argsValues.ToArray());
}
}
Method '
CreateSimpleNamedFormat
' doesn't support modifiers. But method '
CreateNamedFormat
' fully supports it. I create two methods intentionally, because their performance differs considerably.
C# 4.0 get us the good feature of extension methods. And we can now easily realize two extension methods for
StringBuilder
.
public static class StringBuilderExtensions {
public static void AppendSimpleNamedFormat(this StringBuilder stringBuilder, string format, params NamedValue[] args) {
stringBuilder.Append(StringFormat.CreateSimpleNamedFormat(format, args));
}
public static void AppendNamedFormat(this StringBuilder stringBuilder, string format, params NamedValue[] args) {
stringBuilder.Append(StringFormat.CreateNamedFormat(format, args));
}
}
Conclusion
Presented methods are very simple. It needs to perform arguments verification and some other tests. In the article, I wanted only to share my idea. I hope this article helps you in your work.