gpt4 book ai didi

c# - 当字符串包含 '{}' 字符时无法组合字符串

转载 作者:行者123 更新时间:2023-12-02 12:00:39 25 4
gpt4 key购买 nike

我正在尝试在 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>\"}"

我得到的错误是:

Input string was not in a correct format.

你能告诉我如何实现字符串插值吗?

最佳答案

大括号需要转义:

string test = string.Format("{{img: \"{0}\", html: \"{1}\"}}", "images/a.png", "<div></div>");

关于c# - 当字符串包含 '{}' 字符时无法组合字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9354561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com