0

How can I pass string template to function?

I`m creating general usage library.

At this point main application needs to give template for email and library must add specific value at specific place in email.

void SomeFunction(string Template)
{
   string OtherString = "This text is inserted";


   string result - how to set the value of this string - Some text This text is inserted aa?
}


string Template = "Some text {need insert here} aa";

SomeFunction(Template);
SpoksST
  • 776
  • 1
  • 8
  • 24

3 Answers3

4

try something like this:

string otherString = "inserted value";
string template = string.Format("Some text {0} aa", otherString);
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
0
string Template = "Some Text {need insert here} aa";

string InYourfunction = Template.Replace("{need insert here}", "whatever you want to replace here with");
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

Are you looking for:

void SomeFunction(string templateString)
{
   string otherString = "This text is inserted";


   string result = string.Format(templateString, otherString);
}


string template = "Some text {0} aa";

SomeFunction(template);


However, why would you do it like this, instead of more simpler and straight-forward option provided by Jens Kloster?

What if you pass an incorrect templateString to your SomeFunction e.g. passing "My test: {0},{1}" while SomeFunction expects only "My test: {0}"?

Community
  • 1
  • 1
publicgk
  • 3,170
  • 1
  • 26
  • 45