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 parameters.
Is it possible to create some form of overload that will accept a syntax similar to this?
String.Format("You are {age} years old and your last name is {name} ", {age = "18", name = "Foo"});
The string format() method formats the given string into a nicer output in Python. The syntax of the format() method is: template. format(p0, p1, ..., k0=v0, k1=v1, ...)
The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.
Format provides give you great flexibility over the output of the string in a way that is easier to read, write and maintain than just using plain old concatenation. Additionally, it's easier to get culture concerns right with String.
How about the following, which works both for anonymous types (the example below), or regular types (domain entities, etc):
static void Main() { string s = Format("You are {age} years old and your last name is {name} ", new {age = 18, name = "Foo"}); }
using:
static readonly Regex rePattern = new Regex( @"(\{+)([^\}]+)(\}+)", RegexOptions.Compiled); static string Format(string pattern, object template) { if (template == null) throw new ArgumentNullException(); Type type = template.GetType(); var cache = new Dictionary<string, string>(); return rePattern.Replace(pattern, match => { int lCount = match.Groups[1].Value.Length, rCount = match.Groups[3].Value.Length; if ((lCount % 2) != (rCount % 2)) throw new InvalidOperationException("Unbalanced braces"); string lBrace = lCount == 1 ? "" : new string('{', lCount / 2), rBrace = rCount == 1 ? "" : new string('}', rCount / 2); string key = match.Groups[2].Value, value; if(lCount % 2 == 0) { value = key; } else { if (!cache.TryGetValue(key, out value)) { var prop = type.GetProperty(key); if (prop == null) { throw new ArgumentException("Not found: " + key, "pattern"); } value = Convert.ToString(prop.GetValue(template, null)); cache.Add(key, value); } } return lBrace + value + rBrace; }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With