C# 如何在 .NET 中的格式字符串中转义大括号(大括号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/91362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:32:36  来源:igfitidea点击:

How to escape braces (curly brackets) in a format string in .NET

提问by Pop Catalin

How can brackets be escaped in using string.Format.

如何在使用中转义括号string.Format

For example:

例如:

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

This example doesn't throw an exception, but outputs the string foo {0}.

此示例不会引发异常,但会输出 string foo {0}

Is there a way to escape the brackets?

有没有办法逃避括号?

采纳答案by Jorge Ferreira

For you to output foo {1, 2, 3}you have to do something like:

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

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

To output a {you use {{and to output a }you use }}.

输出一个{你用的{{,输出一个}你用的}}

or Now, you can also use c# string interpolation like this (feature available in C# 6.0)

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

Escaping Brackets: String Interpolation $(""). it is new feature in C# 6.0

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

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

回答by Wolfwyrd

Almost there! The escape sequence for a brace is {{or }}so for your example you would use:

差不多好了!为支柱的转义序列{{}}使你的例子中,你可以使用:

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

回答by elec

You can use double open brackets and double closing brackets which will only show one bracket on your page.

您可以使用双左括号和双右括号,它们只会在您的页面上显示一个括号。

回答by Guru Kara

Yes to output {in string.Formatyou have to escape it like this {{

是的要输出{string.Format你必须像这样逃避它{{

So this

所以这

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

will output "foo {1,2,3}".

将输出"foo {1,2,3}".

BUT you have to know about a design bug in C# which is that by going on the above logic you would assume this below code will print {24.00}

但是您必须了解 C# 中的一个设计错误,即通过执行上述逻辑,您会假设下面的代码将打印 {24.00}

int i = 24;
string str = String.Format("{{{0:N}}}", i); //gives '{N}' instead of {24.00}

But this prints {N}. This is because the way C# parses escape sequences and format characters. To get the desired value in the above case you have to use this instead.

但这会打印 {N}。这是因为 C# 解析转义序列和格式字符的方式。要在上述情况下获得所需的值,您必须改用它。

String.Format("{0}{1:N}{2}", "{", i, "}") //evaluates to {24.00}

Reference Articles String.Format gottachand String Formatting FAQ

参考文章 String.Format gottachString Formatting FAQ

回答by pomber

[TestMethod]
public void BraceEscapingTest()
{
    var result = String.Format("Foo {{0}}", "1,2,3");  //"1,2,3" is not parsed
    Assert.AreEqual("Foo {0}", result);

    result = String.Format("Foo {{{0}}}", "1,2,3");
    Assert.AreEqual("Foo {1,2,3}", result);

    result = String.Format("Foo {0} {{bar}}", "1,2,3");
    Assert.AreEqual("Foo 1,2,3 {bar}", result);

    result = String.Format("{{{0:N}}}", 24); //24 is not parsed, see @Guru Kara answer
    Assert.AreEqual("{N}", result);

    result = String.Format("{0}{1:N}{2}", "{", 24, "}");
    Assert.AreEqual("{24.00}", result);

    result = String.Format("{{{0}}}", 24.ToString("N"));
    Assert.AreEqual("{24.00}", result);
}

回答by Adam Cox

Came here in search of how to build json strings ad-hoc (without serializing a class/object) in C#. In other words, how to escape braces and quotes while using Interpolated Strings in C#and "verbatim string literals" (double quoted strings with '@' prefix), like...

来到这里寻找如何在 C# 中临时构建 json 字符串(无需序列化类/对象)。换句话说,如何在 C# 中使用内插字符串和“逐字字符串文字”(带有 '@' 前缀的双引号字符串)时转义大括号和引号,例如...

var json = $@"{{""name"":""{name}""}}";

回答by SliverNinja - MSFT

Escaping curly bracketsAND using string interpolationmakes for an interesting challenge. You need to use quadruple bracketsto escape the string interpolationparsing and string.formatparsing.

转义大括号和使用字符串插值是一个有趣的挑战。您需要使用四重括号来转义字符串插值解析和string.format解析。

Escaping Brackets: String Interpolation $("") and String.Format

转义括号:字符串插值 $("") 和 String.Format

string localVar = "dynamic";
string templateString = $@"<h2>{0}</h2><div>this is my {localVar} template using a {{{{custom tag}}}}</div>";
string result = string.Format(templateString, "String Interpolation");

// OUTPUT: <h2>String Interpolation</h2><div>this is my dynamic template using a {custom tag}</div>

回答by Aarif

or you can use c# string interpolation like this (feature available in C# 6.0)

或者您可以像这样使用 c# 字符串插值(C# 6.0 中可用的功能)

var value = "1, 2, 3";
var output = $" foo {{{value}}}";

回答by Goldfish

My objective:

我的目标:

I needed to assign the value "{CR}{LF}"to a stringvariable delimiter.

我需要将值分配"{CR}{LF}"给一个string变量delimiter

Code c#:

代码 C#:

string delimiter= "{{CR}}{{LF}}";

Note: To escape special characters normally you have to use . For opening curly bracket {, use one extra like {{. For closing curly bracket }, use one extra }}.

注意:要正常转义特殊字符,您必须使用 . 要打开大括号 {,请使用额外的 {{。对于结束大括号},使用一个额外的}}。

回答by Manish Kumar Gurjar

Escaping Brackets: String Interpolation $("") :

转义括号:字符串插值 $("") :

Now, you can also use c# string interpolation like this (feature available in C# 6.0)

现在,您还可以像这样使用 c# 字符串插值(C# 6.0 中提供的功能)

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