How to get String.Format not to parse {0}

岁酱吖の 提交于 2019-12-06 07:59:09
Florian Reischl

Use double curly braces:

string result = string.Format("{{Ignored}} {{123}} {0}", 543);

Curly braces can be escaped by using two curly brace characters. The documentation of string.Format states:

To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

In your example that would be

sp.AppendFormat("return String.Format(\"{0} = '{{0}}'\",cmbList.SelectedValue);", 
    columnName);
sp.AppendFormat("return String.Format(\"{0} = '{{0}}'\", cmbList.SelectedValue);", columnName);

Use "{{0}}". This will result in the string "{0}"

String.Format(\"{0} = '{{0}}'\",
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!