Question

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, 
                              "}");   
Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top