Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape curly brace '{' in String.Format [duplicate]

Tags:

string

c#

.net

How do I display a literal curly brace character when using the String.Format method?

Example:

sb.AppendLine(String.Format("public {0} {1} { get; private set; }",  prop.Type, prop.Name)); 

I would like the output to look like this:

public Int32 MyProperty { get; private set; } 
like image 833
PhilB Avatar asked Sep 22 '10 21:09

PhilB


People also ask

How do you escape curly braces in string format?

Format strings contain “replacement fields” surrounded by curly braces {} . Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} .

How do you escape curly braces in HTML?

Use "{{ '{' }}") to escape it.)

What do {} do in Python?

In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

How do you use curly braces in Python string?

The curly braces show where the inserted value should go. You can insert more than one value. The values do not have to be strings, they can be numbers and other Python objects. >>>


1 Answers

Use double braces {{ or }} so your code becomes:

sb.AppendLine(String.Format("public {0} {1} {{ get; private set; }}",  prop.Type, prop.Name));  // For prop.Type of "Foo" and prop.Name of "Bar", the result would be: // public Foo Bar { get; private set; } 
like image 135
Richard Cook Avatar answered Dec 08 '22 17:12

Richard Cook