将字符串模板传递给函数c#
如何将字符串模板传递给函数 我正在创建通用库 此时,主应用程序需要为电子邮件提供模板,库必须在电子邮件中的特定位置添加特定值将字符串模板传递给函数c#,c#,string,C#,String,如何将字符串模板传递给函数 我正在创建通用库 此时,主应用程序需要为电子邮件提供模板,库必须在电子邮件中的特定位置添加特定值 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? } st
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);
试着这样做:
string otherString = "inserted value";
string template = string.Format("Some text {0} aa", otherString);
试着这样做:
string otherString = "inserted value";
string template = string.Format("Some text {0} aa", otherString);
您正在寻找:
void SomeFunction(string templateString)
{
string otherString = "This text is inserted";
string result = string.Format(templateString, otherString);
}
string template = "Some text {0} aa";
SomeFunction(template);
但是,为什么您要这样做,而不是由提供更简单、更直接的选项 如果您将错误的templateString传递给SomeFunction,例如传递“我的测试:{0},{1}”,而SomeFunction只需要“我的测试:{0}”,该怎么办?您在寻找:
void SomeFunction(string templateString)
{
string otherString = "This text is inserted";
string result = string.Format(templateString, otherString);
}
string template = "Some text {0} aa";
SomeFunction(template);
但是,为什么您要这样做,而不是由提供更简单、更直接的选项 如果将不正确的templateString传递给SomeFunction,例如传递“我的测试:{0},{1}”,而SomeFunction只需要“我的测试:{0}”,该怎么办?检查此项:检查此项: