Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using brackets in String: "Input string was not in a correct format" [duplicate]

How can I format a String in C# where the pattern has brackets? When I run the following statement...

String.Format("Foo { Bar={0} }", this.Bar);

... I receive a runtime exception:

System.FormatException: Input string was not in a correct format.

Should I have to escape the brackets? How to?

like image 570
Rubens Mariuzzo Avatar asked Nov 28 '22 08:11

Rubens Mariuzzo


2 Answers

Escape the brackets by doubling the brackets like {{ and }}

String.Format("Foo {{ Bar={0} }}", this.Bar);
like image 68
Dustin Kingen Avatar answered Jan 11 '23 23:01

Dustin Kingen


Try using double curly braces, so it looks like:

String.Format("Foo {{ Bar={0} }}", this.Bar);

Looks like it has already been answered: Escape curly brace '{' in String.Format

like image 34
uv_man Avatar answered Jan 12 '23 00:01

uv_man