Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

c#

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"}); 
like image 781
Espo Avatar asked Aug 24 '09 12:08

Espo


People also ask

What is format string syntax?

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, ...)

What is the correct syntax to format the string in Python?

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.

Why do we use formatting 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.


1 Answers

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;     }); } 
like image 98
Marc Gravell Avatar answered Sep 28 '22 15:09

Marc Gravell