Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Formating output text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joza - 03 Apr 2008 20:53 GMT
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?
Ignacio Machin ( .NET/ C# MVP ) - 03 Apr 2008 22:48 GMT
> Hi!
>
[quoted text clipped - 19 lines]
>
> 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.
Joza - 03 Apr 2008 23:01 GMT
> 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 - 03 Apr 2008 23:02 GMT
> 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... :(
Claes Bergefall - 04 Apr 2008 10:27 GMT
>> 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
[quoted text clipped - 14 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.