gpt4 book ai didi

c# - 如何在.NET中以格式字符串转义大括号(大括号)

转载 作者:bug小助手 更新时间:2023-10-28 10:41:45 25 4
gpt4 key购买 nike

使用string.Format如何对括号进行转义?

例如:

String val = "1,2,3"
String.Format(" foo {{0}}", val);

这个例子没有抛出异常,而是输出字符串foo {0}

有没有办法逃脱括号?

最佳答案

要输出 foo {1, 2, 3},您必须执行以下操作:

string t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t);

要输出一个 { 你使用 {{ 并且要输出一个 } 你使用 }}

或者现在,您也可以像这样使用 C# 字符串插值(C# 6.0 中提供的一项功能)

转义括号:字符串插值 $("")。这是 C# 6.0 的新特性。

var inVal = "1, 2, 3";
var outVal = $" foo {{{inVal}}}";
// The output will be: foo {1, 2, 3}

关于c# - 如何在.NET中以格式字符串转义大括号(大括号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/91362/

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