C# 多行逐字字符串中 string.Format 的转义参数
string template = @"
{
argument1 = ""{0}"";
argument2 = {1};
}";
当我使用 string.Format 将其格式化为普通字符串时,自然会出现输入字符串格式不正确的异常。我尝试按照 msdn 文档中的建议转义参数,例如“{{0}}”甚至“{{{0}}}”,但我仍然遇到相同的异常。关于如何格式化这样的字符串有什么想法吗?
谢谢!
PS[编辑] 我的原始字符串用于生成 WCAT 场景文件:
string scenarioHeaderTemplate = @"
scenario
{{
name = ""WCAT Scenario"";
warmup = {0};
duration = {1};
cooldown = {2};
default
{
version = HTTP11;
setheader
{
name = ""Connection"";
value = ""keep-alive"";
}
statuscode = 200;
close = ka;
}
}}";
如果我尝试 string.Format(scenarioHeaderTemplate, 10, 10, 10); 它会抛出异常
string template = @"
{
argument1 = ""{0}"";
argument2 = {1};
}";
When I format it as a usual string with string.Format, naturally i get an exception that the input string was not in correct format. I tried escaping the arguments as it is recommended in msdn documentation, like "{{0}}" and even "{{{0}}}", but i still get the same exception. Any ideas on how to format such a string?
Thanks!
P.S.[edit] my original string is for generating a WCAT scenario file:
string scenarioHeaderTemplate = @"
scenario
{{
name = ""WCAT Scenario"";
warmup = {0};
duration = {1};
cooldown = {2};
default
{
version = HTTP11;
setheader
{
name = ""Connection"";
value = ""keep-alive"";
}
statuscode = 200;
close = ka;
}
}}";
and it throws if i try string.Format(scenarioHeaderTemplate, 10, 10, 10);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于左大括号和右大括号。您需要引用这些内容,否则 Format 会认为您正在开始参数说明符。
The problem is the open and close braces. You need to quote those, or Format will think you're begining a parameter specifier.