snippetcsharpCritical
How to escape braces (curly brackets) in a format string in .NET
Viewed 0 times
nethowstringcurlybracesformatbracketsescape
Problem
How can brackets be escaped in using
For example:
This example doesn't throw an exception, but it outputs the string
Is there a way to escape the brackets?
string.Format?For example:
String val = "1,2,3"
String.Format(" foo {{0}}", val);This example doesn't throw an exception, but it outputs the string
foo {0}.Is there a way to escape the brackets?
Solution
For you to output
To output a
Or now, you can also use C# string interpolation like this (a feature available in C# 6.0)
Escaping brackets: String interpolation $(""). It is new feature in C# 6.0.
foo {1, 2, 3} you have to do something like: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 (a feature available in C# 6.0)
Escaping brackets: String interpolation $(""). It is new feature in C# 6.0.
var inVal = "1, 2, 3";
var outVal = $" foo {{{inVal}}}";
// The output will be: foo {1, 2, 3}Code Snippets
string t = "1, 2, 3";
string v = String.Format(" foo {{{0}}}", t);var inVal = "1, 2, 3";
var outVal = $" foo {{{inVal}}}";
// The output will be: foo {1, 2, 3}Context
Stack Overflow Q#91362, score: 1504
Revisions (0)
No revisions yet.