HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

Escape curly brace '{' in String.Format

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
bracestringcurlyformatescape

Problem

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; }

Solution

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; }

Code Snippets

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; }

Context

Stack Overflow Q#3773857, score: 1617

Revisions (0)

No revisions yet.