字符串中带有花括号的C#String.Format [英] C# String.Format with Curly Bracket in string
本文介绍了字符串中带有花括号的C#String.Format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Possible Duplicate:
Escape curly brace '{' in String.Format
c#具有String.Format方法,该方法允许您格式化字符串,但使用标记{0}
{1}
c# has a String.Format method that allows you to format a string but inserting params with the tokens {0}
{1}
我正在尝试创建一个简单的json字符串,该字符串要求大括号包含在字符串中,因此它破坏了格式化程序
I am trying to create a simple json string which requires curly brackets to be in the string, and so it is breaking the formatter
String.Format("{ foo:'{0}', bar:'{1}' }", foo, bar);
在大括号之前添加转义字符没有帮助
Adding an escape before the braces did not help
引发异常,说明我的字符串格式错误,有人知道如何解决此问题吗?
Throws a exception saying my string is incorrectly formatted, anyone know how to get around this?
推荐答案
您可以通过在格式字符串中将括号加倍来逃避括号:
You can escape the braces by doubling them up in your format strings:
string.Format("{{ foo: '{0}', bar: '{1}' }}", foo, bar);
这篇关于字符串中带有花括号的C#String.Format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文