How to escape braces (curly brackets) in a format string in .NET

后端 未结 10 2311
不思量自难忘°
不思量自难忘° 2020-11-22 02:44

How can brackets be escaped in using string.Format.

For example:

String val = \"1,2,3\"
String.Format(\" foo {{0}}\", val); 

相关标签:
10条回答
  • 2020-11-22 03:09

    Came here in search of how to build json strings ad-hoc (without serializing a class/object) in C#. In other words, how to escape braces and quotes while using Interpolated Strings in C# and "verbatim string literals" (double quoted strings with '@' prefix), like...

    var json = $@"{{""name"":""{name}""}}";
    
    0 讨论(0)
  • 2020-11-22 03:14

    Escaping curly brackets AND using string interpolation makes for an interesting challenge. You need to use quadruple brackets to escape the string interpolation parsing and string.format parsing.

    Escaping Brackets: String Interpolation $("") and String.Format

    string localVar = "dynamic";
    string templateString = $@"<h2>{0}</h2><div>this is my {localVar} template using a {{{{custom tag}}}}</div>";
    string result = string.Format(templateString, "String Interpolation");
    
    // OUTPUT: <h2>String Interpolation</h2><div>this is my dynamic template using a {custom tag}</div>
    
    0 讨论(0)
  • 2020-11-22 03:19

    For you to output 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 (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}}}";
    //Output will be:  foo {1, 2, 3}
    
    0 讨论(0)
  • 2020-11-22 03:20

    Yes to output { in string.Format you have to escape it like this {{

    So this

    String val = "1,2,3";
    String.Format(" foo {{{0}}}", val);
    

    will output "foo {1,2,3}".

    BUT you have to know about a design bug in C# which is that by going on the above logic you would assume this below code will print {24.00}

    int i = 24;
    string str = String.Format("{{{0:N}}}", i); //gives '{N}' instead of {24.00}
    

    But this prints {N}. This is because the way C# parses escape sequences and format characters. To get the desired value in the above case you have to use this instead.

    String.Format("{0}{1:N}{2}", "{", i, "}") //evaluates to {24.00}
    

    Reference Articles String.Format gottach and String Formatting FAQ

    0 讨论(0)
  • 2020-11-22 03:26

    You can use double open brackets and double closing brackets which will only show one bracket on your page.

    0 讨论(0)
  • 2020-11-22 03:29

    Escaping Brackets: String Interpolation $("") :

    Now, you can also use c# string interpolation like this (feature available in C# 6.0)

    var inVal= "1, 2, 3";
    var outVal= $" foo {{{inVal}}}";
    //Output will be:  foo {1, 2, 3}
    
    0 讨论(0)
提交回复
热议问题