Instead of using {0} {1}
, etc. I want to use {title}
instead. Then fill that data in somehow (below I used a Dictionary
). This code is invalid and throws an exception. I wanted to know if i can do something similar to what i want. Using {0 .. N}
is not a problem. I was just curious.
Dictionary<string, string> d = new Dictionary<string, string>();
d["a"] = "he";
d["ba"] = "llo";
d["lol"] = "world";
string a = string.Format("{a}{ba}{lol}", d);
Since C# 6 released you are able to use String Interpolation feature
Code that solves your question:
Here is a nice solution that is very useful when formatting emails: http://www.c-sharpcorner.com/UploadFile/e4ff85/string-replacement-with-named-string-placeholders/
Edited:
Example usage:
No, but this extension method will do it
(your Dictionary + foreach + string.Replace) wrapped in a sub-routine or extension method?
Obviously unoptimized, but...
Check this one, it supports formating:
How to use:
Phil Haack discussed several methods of doing this on his blog a while back: http://haacked.com/archive/2009/01/14/named-formats-redux.aspx. I've used the "Hanselformat" version on two projects with no complaints.