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

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

Output:

For key = "rtf" value = wordpad.exe

What does {0} mean?

+61
string c # formatting
Feb 09 '09 at 23:28
source share
6 answers

You print a formatted string. {0} means insert the first parameter following the format string; in this case, the value associated with the rtf key.

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

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

you will create the line "This is a test . Value 42 ".

You can also use expressions and print values ​​several times:

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

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

More details at http://msdn.microsoft.com/en-us/library/txafckwd.aspx , which talks about compound formatting.

+141
Feb 09 '09 at 23:29
source share

This is a placeholder in a string.

For example,

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

will produce this conclusion:

 Hello world. 

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

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

And you will still get the same result.

+18
Feb 09 '09 at 23:33
source share

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

There are many built-in format specifiers, and you can also create your own. For a decent tutorial / list, see Formatting Strings in C # . In addition, there is a FAQ .

+9
Feb 10 '09 at 0:19
source share

For future use in Visual Studio, you can try placing the cursor in the method name (for example, WriteLine) and press F1 to get help in this context. Then digging should find you String.Format() in this case with lots of useful information.

Please note that highlighting a selection (for example, double-clicking or performing a drag and drop) and pressing F1 only searches for non-contextual lines (which usually suck in search of something useful), make sure that you simply place the cursor anywhere in the word, without highlighting it.

It is also useful for documentation on classes and other types.

+6
Feb 10 '09 at 17:35
source share

This is a placeholder for the first parameter, which in your case evaluates to "wordpad.exe".

If you have an extra parameter, you must use {1} , etc.

+5
Feb 09 '09 at 23:30
source share

This placeholder for the parameter, very similar to the %s format specifier, acts within printf .

You can start adding extra things in there to define the format, although this matters more with a numeric variable ( examples here ).

+4
09 Feb '09 at 23:30
source share



All Articles