Formating output text

  • Thread starter Thread starter Joza
  • Start date Start date
J

Joza

Hi!

I need to write some strings in text file, it consist 3 columns and it
must looks like:

ID Name Points
======================
1 Mike 4,5
2 Joseph 1,2
3 Freddy 12,3

The data is in listview control and i know how to get it from it
but when when I put this records in file i can't make to format
these record. I try to use .PadLeft and .PadRight but all i get is
something like this:

ID Name Points
======================
1 Mike 4,5
2 Joseph 1,2
3 Freddy 12,3

How to make that all columns are alligned?
 
Hi!

I need to write some strings in text file, it consist 3 columns and it
must looks like:

ID      Name    Points
======================
1       Mike       4,5
2       Joseph     1,2
3       Freddy    12,3

The data is in listview control and i know how to get it from it
but when when I put this records in file i can't make to format
these record. I try to use .PadLeft and .PadRight but all i get is
something like this:

ID      Name    Points
======================
1       Mike       4,5
2       Joseph      1,2
3       Freddy      12,3

How to make that all columns are alligned?

There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.
 
Ignacio said:
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.

I try like this:

string var="";
string txt1 = "ABCD";
string txt2 = "EFGHIJK";
string num1 = "2.4";
string num2 = "6.5";
int myFieldSize = 35;

Console.WriteLine(txt1 + var.PadRight(myFieldSize - num1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - num2.Length) + num2);

And the result is something like:

ABCD 2.4
EFGHIJK 6.5

instead of

ABCD 2.4
EFGHIJK 6.5

I just don't get it... :(
 
Ignacio said:
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.

I try like this:

string var="";
string txt1 = "ABCD";
string txt2 = "EFGHIJK";
string num1 = "2.4";
string num2 = "6.5";
int myFieldSize = 35;

Console.WriteLine(txt1 + var.PadRight(myFieldSize - num1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - num2.Length) + num2);

And the result is something like:

ABCD 2.4
EFGHIJK 6.5

instead of

ABCD 2.4
EFGHIJK 6.5

I just don't get it... :(
 
Joza said:
Ignacio said:
There are not alligned in the "corrected" example to begin with.
In any case you will have to do it manually, I know of no provider
that does this formatting (fixed length fields).
It's very easy in fact, all you have to do is pritn the field value,
and pad it with white spaces ( [size of the field] - [size of the
actual value] ). Of course you have to check for overflow.

I try like this:

string var="";
string txt1 = "ABCD";
string txt2 = "EFGHIJK";
string num1 = "2.4";
string num2 = "6.5";
int myFieldSize = 35;

Console.WriteLine(txt1 + var.PadRight(myFieldSize - num1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - num2.Length) + num2);

You need to use the first column when calculating how much padding to add,
i.e.:
Console.WriteLine(txt1 + var.PadRight(myFieldSize - txt1.Length) + num1);
Console.WriteLine(txt2 + var.PadRight(myFieldSize - txt2.Length) + num2);

Also note that this will only align correctly if you view it using a
fixed-width font

/claes
 

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

Back
Top