gpt4 book ai didi

c# - C# 中双引号中的 String.Format

转载 作者:行者123 更新时间:2023-11-30 14:12:50 27 4
gpt4 key购买 nike

有没有人可以帮我解决下一行的 string.Format 问题。在“2056”中,我需要作为 {0} 传递。

string body = @"{""idTimeSerie"":""2056"",""idTso"":-1}";

由于双引号,我无法让它执行。

我试过这种方式,但没有成功。

string body = string.Format
(@"{""idTimeSerie"": "" \"{0}\" "",""idTso"":-1}", countryID);

最佳答案

你必须避开花括号

replace { to {{

string body = @"{{""idTimeSerie"":""2056"",""idTso"":-1}}";

编辑:From MSDN - Another way of Escaping

Opening and closing braces are interpreted as starting and ending a format item. Consequently, you must use an escape sequence to display a literal opening brace or closing brace. Specify two opening braces ("{{") in the fixed text to display one opening brace ("{"), or two closing braces ("}}") to display one closing brace ("}"). Braces in a format item are interpreted sequentially in the order they are encountered. Interpreting nested braces is not supported.

int value = 6324;
string output = string.Format("{0}{1:D}{2}",
"{", value, "}");
Console.WriteLine(output);
// The example displays the following output:
// {6324}

关于c# - C# 中双引号中的 String.Format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16150799/

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