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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/530539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:59:37  来源:igfitidea点击:

What does {0} mean when found in a string in C#?

c#stringformatting

提问by Ricardo

In a dictionary like this:

在这样的字典中:

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

The output is:

输出是:

For Key = "rtf" value = wordpad.exe

对于 Key = "rtf" value = wordpad.exe

What does the {0}mean?

这是什么{0}意思?

采纳答案by Daniel LeCheminant

You are printing a formatted string. The {0} means to insert the first parameter following the format string; in this case the value associated with the key "rtf".

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

For String.Format, which is similar, if you had something like

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

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

you'd create a string "This is a test. The value is 42".

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

You can also use expressions, and print values out multiple times:

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

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

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

产生 "Fib: 1, 1, 2, 3"

See more at http://msdn.microsoft.com/en-us/library/txafckwd.aspx, which talks about composite formatting.

http://msdn.microsoft.com/en-us/library/txafckwd.aspx 上查看更多,其中讨论了复合格式。

回答by Bravax

It's a placeholder for the first parameter, which in your case evaluates to "wordpad.exe".

它是第一个参数的占位符,在您的情况下评估为“wordpad.exe”。

If you had an additional parameter, you'd use {1}, etc.

如果你有一个额外的参数,你会使用{1},等等。

回答by Mat

It's a placeholder for a parameter much like the %sformat specifier acts within printf.

它是一个参数的占位符,就像%s格式说明符在printf.

You can start adding extra things in there to determine the format too, though that makes more sense with a numeric variable (examples here).

您也可以开始在那里添加额外的东西来确定格式,尽管这对数字变量更有意义(示例在这里)。

回答by Steven DeWitt

It's a placeholder in the string.

它是字符串中的占位符。

For example,

例如,

string b = "world.";

Console.WriteLine("Hello {0}", b);

would produce this output:

会产生这个输出:

Hello world.

Also, you can have as many placeholders as you wish. This also works on String.Format:

此外,您可以拥有任意数量的占位符。这也适用于String.Format

string b = "world.";
string a = String.Format("Hello {0}", b);

Console.WriteLine(a);

And you would still get the very same output.

你仍然会得到完全相同的输出。

回答by Muad'Dib

In addition to the value you wish to print, the {0} {1}, etc., you can specify a format. For example, {0,4}will be a value that is padded to four spaces.

除了要打印的值{0} {1}、 等,您还可以指定格式。例如,{0,4}将是一个填充到四个空格的值。

There are a number of built-in format specifiers, and in addition, you can make your own. For a decent tutorial/list see String Formatting in C#. Also, there is a FAQ here.

有许多内置的格式说明符,此外,您可以制作自己的格式说明符。有关体面的教程/列表,请参阅C# 中的字符串格式。此外,还有一个常见问题在这里

回答by Rob Parker

For future reference, in Visual Studio you can try placing the cursor in the method name (for example, WriteLine) and press F1to pull up help on that context. Digging around should then find you String.Format()in this case, with lots of helpful information.

为了将来参考,在 Visual Studio 中,您可以尝试将光标放在方法名称(例如,WriteLine)中,然后按F1以调出该上下文的帮助。String.Format()在这种情况下,四处挖掘应该会找到你,其中有很多有用的信息。

Note that highlighting a selection (for example, double-clicking or doing a drag-select) and hitting F1only does a non-context string search (which tends to suck at finding anything helpful), so make sure you just position the cursor anywhere inside the word without highlighting it.

请注意,突出显示一个选择(例如,双击或进行拖动选择)并点击F1只会进行非上下文字符串搜索(这往往很难找到任何有用的东西),因此请确保将光标放在里面的任何位置这个词没有突出显示。

This is also helpful for documentation on classes and other types.

这对于类和其他类型的文档也很有帮助。

回答by sujith karivelil

This is what we called Composite Formattingof the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. The resulting string is written to the output stream.

这就是我们所说的 .NET Framework 的复合格式,用于将对象的值转换为其文本表示并将该表示嵌入到字符串中。结果字符串被写入输出流。

The overloaded Console.WriteLine Method (String,?Object)Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream using the specified format information.

重载的Console.WriteLine 方法 (String,?Object)使用指定的格式信息将指定对象的文本表示(后跟当前行终止符写入标准输出流。