【发布时间】:2017-01-18 23:14:00
【问题描述】:
我需要向字符串列表的(字符串)成员提供可变数据位(电子邮件的“正文”),并尝试使用 string.format 构建它,但我得到,“) 预期”在此处的“http”部分:
string htmlBodyAmalgamation = string.Format(@"<html><body><img src=\"http://www.platypus.com/wp-
content/themes/platypus/images/pa_logo_notag.png\" alt=\"Platypus logo\" width=\"199\" height=\"130\" ><p>{0}</p>", body);
无论“http”前面有 0、1、2 还是 3 个回击(“\”),我都会收到错误消息。
如果没有可变部分(如果主体是静态的/预先知道的)我可以这样做:
List<String> htmlBody = new List<string>
{
"<html><body><img src=\"http://www.platypus.com/wp-content/themes/platypus/images/pa_logo_notag.png\" alt=\"Platypus logo\" width=\"199\" height=\"130\" ><p>Your Platypus Price Push report is attached.</p>",
"</body></html>"
};
mailItem.HTMLBody = string.Join(Environment.NewLine, htmlBody.ToArray());
...它工作正常。
所以它试图通过 string.format 嵌入变量“body”值,这证明是有问题的:
string htmlBodyAmalgamation = string.Format(@"<html><body><img src=\"http://www.platypus.com/wp-content/themes/platypus/images/pa_logo_notag.png\" alt=\"Platypus logo\" width=\"199\" height=\"130\" ><p>Your Platypus Price Push report is attached.</p>", body);
List<String> htmlBody = new List<string>
{
htmlBodyAmalgamation,
"</body></html>"
};
让 html 和 string.format 一起工作的诀窍是什么?
【问题讨论】:
-
在最后一个示例中,您使用逐字运算符
@并使用\ 转义。在这些字符串中,您实际上可以使用"转义",使其变为""。 -
使用单引号似乎更简单,而且似乎有效。
标签: c# html winforms outlook string-formatting