我一直在仔细阅读这篇SO文章,尝试在我的插值字符串周围加上花括号。
string testString = "foo";
string testResult1 = $"{testString}"; // result = "foo" as expected
string testResult2 = $"{{testString}}"; // result = "{testString}" - UH OH
我的期望结果是 "{foo}",针对
testResult2
。我已经尝试使用反斜杠转义外部花括号,但这并不起作用,而且我也没指望它有用。如何在插值字符串变量周围放置文字花括号?一个更准确的例子是:string testResult3 = $"I want to eat some {{testString}} please.";
期望输出结果: "我想要吃一些{foo},请。"
实际输出结果:"我想要吃一些{testString},请。"
如何让它正常工作?(我还尝试了在$
和"
之间加上@
,但是没有成功。)
string testResult2 = $"{{{testString}}}";
来生成{foo}
。 - Chris R. Timmons