我试图在C#中执行字符串插值.我试图组合的输入字符串包含许多"{}"字符(因为它的javascript)似乎导致错误.
为什么我不能在C#中对这些字符串执行字符串插值?
string test = string.Format("{img: \"{0}\", html: \"{1}\"}", "images/a.png", "<div></div>");
// so the output should be
// "{img: \"images/a.png\", html: \"<div></div>\"}"
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
输入字符串的格式不正确.
你能告诉我如何实现我的字符串插值吗?
大括号需要逃脱:
string test = string.Format("{{img: \"{0}\", html: \"{1}\"}}", "images/a.png", "<div></div>");
Run Code Online (Sandbox Code Playgroud)