当stringFormat碰上{和}

常常用到stringFormat,在数据绑定时会用<%#DataBinder.Eval(Container.DataItem,"Date","发布时间:{0:yyyy-MM-dd hh:mm}"%>
在字符串连时string.Format()方法也很方便
string scriptB = "<script language = \"javascript\">";
string scriptE = "</script>";

Page.RegisterClientScriptBlock(ClientID,string.Format(@"{0}
......
{1}
{0}
.......
{1}
"),scriptB,scriptE);


可这个好用的东东微软去设计不够完善

当你的stringFormat语句中本身就有用"{""}"字符,那就麻烦了,本来我想用转义字符"\{"和"\}"
但这样的语句是错误的:"\{ xxxxx{0}yyyyy \}",那么一碰到"{"我们就不能用stringFormat了吗,解决办法其实很简单:
string a = "{";

string b = "}";

那么你就可以写

string.Format("function myfun() {0} xxx;yyy;zzz; {1}", a, b);

问题解决;

补充:"{{"和"}}"也可以解决此问题,这种感觉有点像SQLServer中的语义。

posted @ 2005-04-11 12:12  Think  阅读(1461)  评论(7编辑  收藏  举报