Constant Column in string format

  • Thread starter Thread starter PawelR
  • Start date Start date
P

PawelR

Hello Group,
My apps save to disk (as .txt file) data from Table.
I use formatting string (String.Format("{0}\t{1}\t{2}\t{3},
myTable.ItemArray). In column [1] I've different length of text.
Text is from 1 to 15 chars.

How set in text file constant width for columns.
Thx PawelR
 
You want column 2, 3 and 4 to be neatly aligned in the text file?

Well, you could add blank spaces to the string in column 0 to make it 15
characters long.

String blank = " ";
String firstValue = (string)myRow[0];
firstValue += blank.SubString(0, 15 - firstValue.Length);

String finalString = String.Format("{0}\t{1}\t{2}\t{3}", firstValue,
myRow[1], myRow[2], myRow[3]);

Note, I haven't tested this code, and especially the String.Format may not
work.
 
Hi Pawe³,
Hello Group,
My apps save to disk (as .txt file) data from Table.
I use formatting string (String.Format("{0}\t{1}\t{2}\t{3},
myTable.ItemArray). In column [1] I've different length of text.
Text is from 1 to 15 chars.

How set in text file constant width for columns.
Thx PawelR

Use the simple formatting:

string stringItem=myTable.Rows[0].ItemArray[1];
String.Format("{0,-15}"
, stringItem);

//or

String.Format("{0,15}"
, stringItem);

Greetings/Pozdrowienia

Marcin
 
Back
Top