Escape curly brackets around a template entry when using String.Format [duplicate]

StackOverflow https://stackoverflow.com/questions/13554195

  •  02-12-2021
  •  | 
  •  

Pregunta

Possible Duplicate:
String.Format exception when format string contains “{”

Does following possible using C# String.Format?

Required output "products/item/{itemId}"

I've tried escaping braces but this does not work:

const string itemIdPattern = "itemId";
string result = String.Format("products/item/{{0}}", itemIdPattern);

Preferably something more nice than

string result = String.Format("products/item/{0}{1}{2}", 
                              "{", 
                              itemIdPattern, 
                              "}");   
¿Fue útil?

Solución

You'll need 3 braces per side for that -- 2 for the braces and 1 for the replacement.

string result = String.Format("products/item/{{{0}}}", itemIdPattern);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top