Home > DeveloperSection > Forums > Spacing in String.Format()
Jayden Bell
Jayden Bell

Total Post:108

Posted on    September-23-2013 5:16 AM


 1 Reply(s)
 1036  View(s)
Rate this:

I want fixed spacing between words in a string similar to what you have in a datagrid.

I tried:

string item = String.Format("{0,-9}{1,-42}{2,-24}{3,0}", ++i, itemName, itemQuantity, totalItemPrice.ToString("#,##0.00") + System.Environment.NewLine);

But since the itemName length is varaible itemQuantity and totalItemPrice do not maintain their positions and get pushed forward.

The string is subsequently assigned to a RichTextBox.

Sumit Kesarwani

Total Post:378

Posted on    September-23-2013 7:04 AM

Hi Jayden,

You can do like this:

I have just modified the way you have used the values and I can get the data displayed as data grid.

 string itemName = "phone";

 int itemQuantity = 5, totalItemPrice=50;

 string item = String.Format("{0,-2}{1,-10}{2,-5}{3,-5}", ++i, itemName, itemQuant ity, totalItemPrice.ToString("#,##0.00")+System.Environment.NewLine);

Don't want to miss updates? Please click the below button!

Follow MindStick