format string question

  • Thread starter Thread starter MM
  • Start date Start date
M

MM

Hi,

I have a param class containg these vars:-
string key; // eg: "WN"
object value; // eg: 1.2
string format; // eg "F2"

and I output these to a StreamWriter using

nc_file.Write("{0}{1:" + param.format + "}", param.key, param.value);

to give output like (using above egs) "WN1.20"

This works great for numeric types. Problem is that there are a couple
of string values which require padding - eg: "FN<some filename...."
which requires a fixed field size of say 20.

Is there a way to use a format string to achieve this - if there is I
haven't figured it. 99% of all parameters are string key/ numeric value
and I'm hoping I don't need to use special cases in the enumerator to
catch the few string values that need padding. Hopefully this makes sense.

Thanks for the help. matthew.
 
This works great for numeric types. Problem is that there are a couple
of string values which require padding - eg: "FN<some filename...."
which requires a fixed field size of say 20.
The following code displays a string with a field size of 10. The first
example is right-padded, the second left padded.

Console.WriteLine( "...{0,10}...", "a" );
Console.WriteLine( "...{0,-10}...", "a" );

Is that what you mean?

Greetings,
Wessel
 
Wessel said:
The following code displays a string with a field size of 10. The
first example is right-padded, the second left padded.

Console.WriteLine( "...{0,10}...", "a" );
Console.WriteLine( "...{0,-10}...", "a" );

Is that what you mean?

Greetings,
Wessel

Hi Wessel,

I've been trying Console.WriteLine("...{0:-20}, etc) so that's my
mistake. Thanks alot, matthew.
 
Back
Top