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

后端 未结 10 2537
不思量自难忘°
不思量自难忘° 2020-11-22 02:44

How can brackets be escaped in using string.Format.

For example:

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

10条回答
  •  猫巷女王i
    2020-11-22 03:09

    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...

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

提交回复
热议问题