c# - 在 String.Format 中转义大括号 '{'

标签 c# .net string

<分区>

如何在使用 String.Format 方法时显示文字花括号字符?

例子:

sb.AppendLine(String.Format("public {0} {1} { get; private set; }", 
prop.Type, prop.Name));

我希望输出看起来像这样:

public Int32 MyProperty { get; private set; }

最佳答案

使用双括号 {{}} 使您的代码变为:

sb.AppendLine(String.Format("public {0} {1} {{ get; private set; }}", 
prop.Type, prop.Name));

// For prop.Type of "Foo" and prop.Name of "Bar", the result would be:
// public Foo Bar { get; private set; }

关于c# - 在 String.Format 中转义大括号 '{',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3773857/

相关文章:

c# - Telerik 网格中的多行单元格 (MVC3)

python - 如何在行和列中剪切 python 输出

c# - .NET : How to validate XML file with DTD without DOCTYPE declaration

.net - 在 DbMigration 中访问 Up 里面的数据库名

c - 如何在c中将ssscanf与字符串矩阵一起使用

java - 使用java的ldap搜索过滤器

c# - 如何重置 UINavigationItem.BackBarButtonItem 的 tintColor?

c# - 如何检测窗体任何控件的变化?

c# - Windows Media Player 视频无缝循环

c# - 如何使用汇编版本的预处理器条件?