Format string centering columns

jbl_ks

Member
Joined
Dec 16, 2010
Messages
24
Programming Experience
Beginner
I am outputting a columnar table to a text file, 5 columns each 20 characters wide.
The example below lets me align these columns either left or right.

VB.NET:
Dim strfmt As String = "{0,-20}{1,-20}{2,-20}{3,-20}{4,-20}"
Dim strfmt As String = "{0,20}{1,20}{2,20}{3,20}{4,20}"

"Payment Date    Amount  Paid      Principal Paid      Interest  Paid      Principal  Balance"

I have several long winded ways of creating the title line above these that involve calculating the length of each string, but is there a simple way to 'center align', with each of the 5 title headings centered above the 20 chr wide columns? On a one time deal I can easily just build a string that fits.
 
The formula for calculating the starting position for each title is as follows:

Dim ln, loc as integer
ln = title.Length
loc = ((20 - ln) \ 2) + 1
 
Thanks Solitare,

Thats similar to what I already worked out as a 'manual' way to do it.

I was hoping that there was something simple that I was overlooking.

In the string format sample that I submitted, -20 indicates left aligned field of 20, 20 means right aligned field of 20 and there is aparently no way to use this alone to center the alignment.

They should add +20 to mean a center aligned field of 20. I guess that if I could figure out how to override that function that would make me a pretty smart fellow, too bad I am not.
 
Back
Top