-1

I want to create a string template, part of its content will be completed lately

string myTemplete = "This is a template ,which depends on {1} and {2} and {3}"

After that I have some functions calls chain and I collect the data from them

int arg1 = MyFunc1();
string arg2 = MyFunc2();
string arg3 = MyFunc3();

// need to populate the myTemplete with arg 1 for {0}, arg2 for {1}, arg3 for {2}

How can I do it? The template I build is long and in used in many places so don`t want to make something like that

int arg1 = MyFunc1();
string arg2 = MyFunc2();
string arg3 = MyFunc3();
string myData = string.Format("This is a template ,which depends on {1} and {2} and {3}"
,arg1, arg2, arg3);
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • 1
    Uhm... put the template string inside a constant (or, even better, into [a resource](http://stackoverflow.com/questions/90697/how-to-create-and-use-resources-in-net), so that it can be localized) and then call `string.Format(MyTemplate, MyFunc1(), MyFunc2(), MyFunc3())`? – Heinzi Apr 23 '14 at 07:56

1 Answers1

5

if you want a reproductive string

public static class MyTemplates
{
    public static string MyTemplate(object arg1,object arg2,object arg3)
    {
     return string.Format("This is a template ,which depends on {0} and {1} and {2}",arg1, arg2, arg3);
    }
}

then simply call

string mytemplete = MyTemplates.MyTemplate(MyFunc1(), MyFunc2(),MyFunc3());

EDIT: Changed the string to object as proposed by Tim Schmelter and string format numbers changed due to the copy paste I did

Schuere
  • 1,579
  • 19
  • 33
  • 2
    Change `{1} and {2} and {3}` to `{0} and {1} and {2}`, otherwise it will throw a `FormatException`. I would also allow object as argument since `String.Format` supports it also. – Tim Schmelter Apr 23 '14 at 08:03
  • sorry, simply copy pasted that :s; changed according to your proposition – Schuere Apr 23 '14 at 11:27