using WriteLine

J

Jerry

I am writing text out to a text file and I want to LEFT justify the text.

I wave found examples for the following:

// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:p}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);

But does an option exist to left justify a string? I remember that c/c++
would.

Thanks,

Jerry
 
P

Peter Bradley

MSDN says:

<quote>
Format Item Syntax
Each format item takes the following form.

{index[,alignment][:formatString]}

The matching braces ("{" and "}") are required.

Format Item Components
A format item consists of the following components.

Index Component
The mandatory index component, also called a parameter specifier, is a
number starting from 0 that identifies a corresponding element in the list
of values. That is, the format item whose parameter specifier is 0 formats
the first value in the list, the format item whose parameter specifier is 1
formats the second value in the list, and so on.

Multiple format items can refer to the same element in the list of values by
specifying the same parameter specifier. For example, you can format the
same numeric value in hexadecimal, scientific, and number format by
specifying a source string like this: "{0:X} {0:E} {0:N}".

Each format item can refer to any parameter. For example, if there are three
values, you can format the second, first, and third value by specifying a
source string like this: "{1} {0} {2}". A value that is not referenced by a
format item is ignored. A runtime exception results if a parameter specifier
designates an item outside the bounds of the list of values.

Alignment Component
The optional alignment component is a signed integer indicating the
preferred formatted field width. If the value of alignment is less than the
length of the formatted string, alignment is ignored and the length of the
formatted string is used as the field width. The formatted data in the field
is right-aligned if alignment is positive, and left-aligned if alignment is
negative. If padding is necessary, white space is used. The comma is
required if alignment is specified.

Format String Component
The optional formatString component consists of standard or custom format
specifiers. If formatString is not specified, the general ("G") format
specifier is used. The colon is required if formatString is specified.
</quote>

Of this, the most relevant bit, I think, would be:

"The formatted data in the field is right-aligned if alignment is positive,
and left-aligned if alignment is negative."

MSDN is your friend. Along with Google, of course.


Peter
 
P

PhilipDaniels

I am writing text out to a text file and I want to LEFT justify the text.
But does an option exist to left justify a string? I remember that c/c++
would.

Thanks,

Jerry

The syntax of a format specifier is

{index[,alignment][:formatString]}.

For example:

Console.WriteLine("**{0,6}**", 22);
prints ** 22**

Console.WriteLine("**{0,-6}**", 22);
prints **22 **
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top