C#:Format Printing
Jump to navigation
Jump to search
The Write
method provides a powerful and succinct mechanism for solving many formatting problems.
The Write method
The Write method takes a formatting string followed by any number of additional parameters. The formatting string includes a number of braces {} - each of these is substituted by the actual parameter given.
{0} is used for the first parameter {1} is used for the second parameter
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
Using Write to align
We can format output involving fixed and variable components. Usually we align strings to the left and numbers to the right.
In the Write
a number of values may follow the format string.
These values are used up by {}
placeholders in the format string.
"Country: {0,-18} GDP: {1,20:n0}\n"
{0,-18}
- 0 means the first parameter is used here
- the minus in
-18
means align left,
the 18
means 18 characters
are to be used, the s
is for string.
{1,20:n0}
- 1 means the second parameter goes here
- 20:n0 means use 20 spaces for the number, use 0 decimal places
\n
::This starts a new line.
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]