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

后端 未结 10 2350
不思量自难忘°
不思量自难忘° 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:32

    My objective:

    I needed to assign the value "{CR}{LF}" to a string variable delimiter.

    Code c#:

    string delimiter= "{{CR}}{{LF}}";
    

    Note: To escape special characters normally you have to use . For opening curly bracket {, use one extra like {{. For closing curly bracket }, use one extra }}.

提交回复
热议问题