Constant Column in string format

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
 
M

Morten Wennevik

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.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

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
 

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

Similar Threads

String Format 1
Format String 2
C# registry read converts string 1
Solution: Parsing a search string and creating an SQL where clause 3
Sort in dataSet 1
string format 9
String Format help 11
Delete row 1

Top