代码之家  ›  专栏  ›  技术社区  ›  Unbreakable

如何在字典键中添加占位符大括号

  •  0
  • Unbreakable  · 技术社区  · 6 年前

    所以,我有一本字典,如下所示:

    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");
    

    但我不想在键中添加大括号。

    我想在我的 格式 逻辑。

    伪代码:

    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())); 
    }
    

    但我不知道一种有效的方法。请引导我。

    3 回复  |  直到 6 年前
        1
  •  3
  •   nvoigt    6 年前

    你需要的时候就加上它?

    public void Format(string rawMsg, Dictionary<string, object> dict)
    {
        var temp = dict.Aggregate(rawMsg, (current, parameter) => current.Replace("{" + parameter.Key + "}", parameter.Value.ToString())); 
    }
    
        2
  •  3
  •   Dmitrii Bychenko    6 年前

    如果你正在创造自己的 字符串插值 我建议 正则表达式 ,例如:

      Dictionary<string, object> dict = new Dictionary<string, object>() {
        {"first", "Value1" },
        {"second", "Value2" },
      };
    
      string rawMsg = "My values are {first} and {second}.";
    
      string result = Regex.Replace(
        rawMsg, 
       @"\{[^{}]+?\}", 
        match => dict[match.Value.Substring(1, match.Value.Length - 2)]?.ToString());
    
      Console.Write(result);
    

    结果:

      My values are Value1 and Value2.
    

    附笔。 我假设你想在 一去

      Dictionary<string, object> dict = new Dictionary<string, object>() {
        {"first", "{second}" },
        {"second", "third" },
      };
    
      string rawMsg = "My values are {{first}} and {second}.";
    

    正确的答案是

      My values are {second} and third.
    

    不(我们不想处理 second 两次 )

      My values are third and third.
    
        3
  •  1
  •   vc 74    6 年前

    这应该有效。简而言之,创建一个regex并将其存储在静态成员中,以便只编译一次,然后使用它用字符串中的值替换占位符引用。( Fiddle )

    private static Regex _regex = new Regex(@"(?<={)(\w+)");
    
    ...
    
    public string Format(string rawMsg, IReadOnlyDictionary<string, object> dict) =>
    
        _regex.Replace(rawMsg, match =>
        {
            string placeHolder = match.ToString();
    
            return dict.TryGetValue(placeHolder, out object placeHolderValue) 
                       ? placeHolderValue.ToString() : null;
        });
    

    请注意,第一个regex组是非捕获的,用于从捕获中排除大括号。