Set String.Format at runtime

c#

Solution

Of course. Format strings are just that, strings.

 string fmt = "{0} - {1}";    // get this from your XML somehow
 string name = "Chris";
 string phone = "1234567";

 string name_with_phone = String.Format(fmt, name, phone);

Just be careful with it, because your end user might be able to disrupt the program. Do not forget to `FormatException`.

Problem

I have an XML File that I want to allow the end user to set the format of a string. ex: ``` <Viewdata> <Format>{0} - {1}</Format> <Parm>Name(property of obj being formatted)</Parm> <Parm>Phone</Parm> </Viewdata> ``` So at runtime I would somehow convert that to a `String.Format("{0} - {1}", usr.Name, usr.Phone);` Is this even possible?

Original source