C#

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

发布于 2021-02-02 16:03:51

在这样的字典中:

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"]);

输出为:

对于Key =“ rtf”,值= wordpad.exe

什么{0}意思

关注者
0
被浏览
207
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

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

    对于String.Format,如果您有类似的内容,则类似

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

    您将创建一个字符串“这 是一个测试 。值为 42 ”。

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

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

    得到“FIB: 1123

    有关更多信息,请参见http://msdn.microsoft.com/zh-
    cn/library/txafckwd.aspx
    ,其中讨论了复合格式。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看