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

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

  •  02-12-2021
  •  | 
  •  

문제

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, 
                              "}");   
도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top