How can I create a more user-friendly string.format syntax?

前端 未结 6 1999
慢半拍i
慢半拍i 2020-11-27 10:29

I need to create a very long string in a program, and have been using String.Format. The problem I am facing is keeping track of all the numbers when you have more than 8-10

6条回答
  •  被撕碎了的回忆
    2020-11-27 11:05

    not quite the same but sort of spoofing it... use an extension method, a dictionary and a little code:

    something like this...

      public static class Extensions {
    
            public static string FormatX(this string format, params KeyValuePair []  values) {
                string res = format;
                foreach (KeyValuePair kvp in values) {
                    res = res.Replace(string.Format("{0}", kvp.Key), kvp.Value.ToString());
                }
                return res;
            }
    
        }
    

提交回复
热议问题