Search code examples
c#messageboxstring.format

Problem with formatting a string with String.Format in C#


I need to print a string in a message box in specific format for which i am using code similar to as shown below:

string text=""; 
for (int i=0; i<n; i++)
{
   a=..
   b=..
   c=..
   text += String.Format("{0, -8} {1,-4} {2,8}", a, b, c);
}
MessageBox.Show(text);

So for following set of values:

XYZ,ABC,100

X,ABC,100

I get following output:

XYZ     ABC     100

X     ABC     100

So you can see the second line is not well formatted. Probably this is happening because i am printing this in MessageBox. The space a character and a 'space' takes is different. Any solution for this?


Solution

  • Try using a \t to insert tabs between values.