Occasionally you want complex format strings. Not large enough or enough of them to use a full blown template system. But rather than
var fields = new { name = "Bob", zipCode = "AB1234", phone = "01234 567890" };
var address = String.Format("{0} : {1}", fields.name, fields.zipCode);
var phone = String.Format("{0} : {1}", fields.name, fields.phone);
it would be nice to write
var fields = new { name = "Bob", zipCode = "AB1234", phone = "01234 567890" };
var address = String.Format("{name} : {zipCode}", fields);
var phone = String.Format("{name} : {phone}", fields);
StringFill provides an extension method Fill
for System.String
and AppendFill
for System.Text.StringBuilder
to provide this:
var sb = new StringBuilder();
sb.AppendFill("Hello {name}", new {name = "World"});
or
string actual = StringFill.Fill("exec SomeProc({arg1}, {arg2});",
new { arg1 = "Test", arg2 = "Example" });
You can grab the code from GitHub.