【发布时间】:2018-09-25 14:39:56
【问题描述】:
所以,我有一本像下面这样的字典:
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("first", "Value1");
dict.Add("second", "Value2");
我有一个如下字符串:
rawMsg = "My values are {first} and {second}.";
预期输出:
My values are Value1 and Value2.
字符串格式化代码:
public void Format(string rawMsg, Dictionary<string, object> dict)
{
var temp = dict.Aggregate(rawMsg, (current, parameter) => current.Replace(parameter.Key, parameter.Value.ToString()));
}
为了使上述代码正常工作,我需要添加如下键:
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("{first}", "Value1"); // Notice the curly braces in Keys
dict.Add("{second}", "Value2");
但我不想在键中添加花括号。
我想在我的 format 逻辑中附加花括号。
伪代码:
public void Format(string rawMsg, Dictionary<string, object> dict)
{
// loop through all the keys and append a curly braces in them.
var temp = dict.Aggregate(rawMsg, (current, parameter) => current.Replace(parameter.Key, parameter.Value.ToString()));
}
但我不知道一种有效的方法。请指导我。
【问题讨论】:
标签: c# .net dictionary