I used have a template string, read from DB and or text file:
var template="Your order {0} has dispatched from {1}, ETA {2:HH:MM dd.MM.yyyy}";
And then used string.Format(template, orderNo, warehouse, eta)
to inject correct values.
Now, a business decision was made, to change those templates to use more meaningful markers:
var template="Your order {orderNo} has dispatched from {warehouse}, ETA {eta:HH:MM dd.MM.yyyy}";
And now I'm not sure what would be the best way to inject values, taking into consideration that, for example, eta
field has formatting that is "hardcoded" into template (so that we could use different DateTime
format depending on language)
One idea was to load the template into StringBuilder
, replace named variables into set of {0}, {1}
.. etc, And then use old string.Format
method, but it's not really scalable and pain in the back to write all the boilerplate.
var formatTemplate = new StringBuilder(template)
.Replace("{orderNo}", "{0}")
.Replace("{warehouse}", "{1}")
.Replace("{eta","{2")
.ToString();
return string.Format(template, orderNo, warehouse, eta)
Is there a better way to do it? Maybe a way to read the template as interpolated string? The string is read from an outside source so I cannot simply use the $"{marker}"
mechanic.