如何获取String.Format不解析{0} [英] How to get String.Format not to parse {0}

查看:376
本文介绍了如何获取String.Format不解析{0}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码生成工具,该工具经常会出现这样的行

I am writing a code generation tool that frequently will have lines like

StringBuilder sp = new Stringbuilder();
sp.AppendFormat("        public {0}TextColumn()\n", className);
sp.AppendLine("        {"
sp.AppendLine("            Column = new DataGridViewTextBoxColumn();");
sp.AppendFormat("            Column.DataPropertyName = \"{0}\";\n", columnName);

但是我运行时遇到的问题

However the issue I am having is when I run in to a line like this.

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

我要第一个 {0} 转到columnName的值,但我希望保留第二个 {0} 内部的 String.Format 会正确处理它。

I want the first {0} to turn in to whatever the value of columnName is but I want the seccond {0} to be left alone so the internal String.Format will process it correctly.

我该怎么做?

推荐答案

使用双花括号:

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

这篇关于如何获取String.Format不解析{0}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆