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);
1

3 Answers 3

4

try something like this:

string otherString = "inserted value";
string template = string.Format("Some text {0} aa", otherString);
Sign up to request clarification or add additional context in comments.

Comments

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

string InYourfunction = Template.Replace("{need insert here}", "whatever you want to replace here with");

Comments

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}"?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.