In C# there is a string interpolation support like this:
$"Constant with {Value}" which will format this string using in-scope variable Value.
But the following won't compile in current C# syntax.
Say, I have a static Dictionary<string, string> of templates:
templates = new Dictionary<string, string> { { "Key1", $"{Value1}" }, { "Key2", $"Constant with {Value2}" } } And then on every run of this method I want to fill in the placeholders:
public IDictionary<string, string> FillTemplate(IDictionary<string, string> placeholderValues) { return templates.ToDictionary( t => t.Key, t => string.FormatByNames(t.Value, placeholderValues)); } Is it achievable without implementing Regex parsing of those placeholders and then a replace callback on that Regex? What are the most performant options that can suit this method as being a hot path?
For example, it is easily achievable in Python:
>>> templates = { "Key1": "{Value1}", "Key2": "Constant with {Value2}" } >>> values = { "Value1": "1", "Value2": "example 2" } >>> result = dict(((k, v.format(**values)) for k, v in templates.items())) >>> result {'Key2': 'Constant with example 2', 'Key1': '1'} >>> values2 = { "Value1": "another", "Value2": "different" } >>> result2 = dict(((k, v.format(**values2)) for k, v in templates.items())) >>> result2 {'Key2': 'Constant with different', 'Key1': 'another'}
formatmust be using a Regular Expression, what do you have against using one?