2

I would like to ignore { character in string is it possible ?

thanks for help

String.Format("function(s, e) {  {0}.PerformCallback(MyObject.GetSelectedItem().value); }", getClientName);
gruber
  • 28,739
  • 35
  • 124
  • 216
  • possible duplicate of [Escape curly brace '{' in String.Format](http://stackoverflow.com/questions/3773857/escape-curly-brace-in-string-format) - to all you "helpful" answerers: http://www.joelonsoftware.com/items/2011/01/05.html :) – bzlm Jan 11 '11 at 14:03
  • 1
    @bzlm, I'll see your Joel, and raise you a [Jeff](http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/) :) – Frédéric Hamidi Jan 11 '11 at 14:11

7 Answers7

6

You have to double the curly braces in order to escape them:

String.Format(CultureInfo.InvariantCulture, @"
    function(s, e) {{
        {0}.PerformCallback(MyObject.GetSelectedItem().value);
    }}", getClientName);
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
2

You can escape the curly brace by typing {{ and it will display as one.

Patrick
  • 17,669
  • 6
  • 70
  • 85
2

Replace your single '{' with a double '{{'.

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
2

The term you're after is 'delimit'. By replacing { with {{ and } with }} like so:

string.Format("function(s, e) {{  {0}.PerformCallback(MyObject.GetSelectedItem().value); }}", getClientName);
MPritchard
  • 7,031
  • 7
  • 28
  • 40
2

To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

Source: String.Format()

unholysampler
  • 17,141
  • 7
  • 47
  • 64
1

If you by ignore, mean that you want to get rid of it, you can use the following code:

myString = myString.Replace("{","");
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
0

I sometimes use

char curly = '{';
String.Format("function(s, e), {0} {1}.PerformCallback(...)", curly, getClientName);

That way the format string is more readable.