c# - 在 C# 中的字符串中找到 {0} 是什么意思?

在这样的字典中:

Dictionary<string, string> openWith = new Dictionary<string, string>();

openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

输出是:

For Key = "rtf" value = wordpad.exe

{0} 是什么意思?

最佳答案

您正在打印一个格式化的字符串。 {0} 表示在格式字符串后面插入第一个参数;在这种情况下,与键“rtf”关联的值。

对于 String.Format,如果你有类似的东西,这是相似的

//            Format string                    {0}           {1}
String.Format("This {0}.  The value is {1}.",  "is a test",  42 ) 

您将创建一个字符串“This is a test。值为 42”。

您还可以使用表达式,并多次打印值:

//            Format string              {0} {1}  {2}
String.Format("Fib: {0}, {0}, {1}, {2}", 1,  1+1, 1+2) 

yielding "Fib: 1, 1, 2, 3"

在 http://msdn.microsoft.com/en-us/library/txafckwd.aspx 上查看更多信息,其中谈到复合格式。

https://stackoverflow.com/questions/530539/

相关文章:

java - 如何在 Java 中舍入整数除法并获得 int 结果?

java - 将 BigDecimal 格式化为最多 2 个十进制数字的字符串,在小数部分删除 0

python - 在 Python 中的文件末尾声明函数

java - 在 SimpleDateFormat 模式字符串中使用字母字符

c - 为什么 %d 代表整数?

excel - 如何在 Excel 中将单元格的字节格式设置为 KB、MB、GB 等?

mysql - 如何在 MySQL 数据库中存储电话号码?

sql - 从 SQL Server 中的十进制中删除尾随零

objective-c - Objective-C 中的舍入数字

r - 如何打印(到纸上)格式良好的数据框