Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting { in verbatim string with C# [duplicate]

Possible Duplicate:
How to escape brackets in a format string in .Net

How do I put { or } in verbatim string in C#?

using System;

class DoFile {

    static void Main(string[] args) {
        string templateString = @"
        \{{0}\}
        {1}
        {2}
        ";
        Console.WriteLine(templateString, "a", "b", "c");
    }
}

I get error when I use '{' or '}' in verbatim string, I guess it's because '{' or '}' is used for parameter marking such as {0}, {1}, {2}. \{ doesn't work.

Unhandled Exception: System.FormatException: Input string was not in a correct format.
  at System.String.ParseFormatSpecifier (System.String str, System.Int32& ptr, System.Int32& n, System.Int32& width, System.Boolean& left_align, System.String& format) [0x00000] in <filename unknown>:0 
  at System.String.FormatHelper (System.Text.StringBuilder result, IFormatProvider provider, System.String format, System.Object[] args) [0x00000] in <filename unknown>:0 
like image 970
prosseek Avatar asked May 13 '11 19:05

prosseek


2 Answers

You just have to escape with double curly brackets..

{{ or }} respectively..

Something like below

string.Format("This is a format string for {0} with {{literal brackets}} inside", "test");

This is a format string for test with {literal brackets} inside

like image 124
Quintin Robinson Avatar answered Sep 28 '22 04:09

Quintin Robinson


You put two like such {{ or }}.

like image 42
Jim Bolla Avatar answered Sep 28 '22 04:09

Jim Bolla