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

时间:2023-01-14 16:10:38

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}是什么意思?

7 个解决方案

#1


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。

#2


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.

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

#3


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#中的字符串格式。另外,这里有一个FAQ。

#4


For future reference, in Visual Studio you can try placing the cursor in the method name (for example, WriteLine) and press F1 to 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 F1 only 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.

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

#5


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}等。

#6


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

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

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

您可以开始在其中添加额外的东西以确定格式,尽管使用数字变量更有意义(此处的示例)。

#7


This is what we called Composite Formatting of 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)使用指定的格式信息将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。

#1


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。

#2


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.

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

#3


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#中的字符串格式。另外,这里有一个FAQ。

#4


For future reference, in Visual Studio you can try placing the cursor in the method name (for example, WriteLine) and press F1 to 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 F1 only 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.

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

#5


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}等。

#6


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

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

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

您可以开始在其中添加额外的东西以确定格式,尽管使用数字变量更有意义(此处的示例)。

#7


This is what we called Composite Formatting of 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)使用指定的格式信息将指定对象的文本表示形式(后跟当前行终止符)写入标准输出流。