Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
Sam Avatar asked Jun 29 '26 10:06

Sam


1 Answers

Try using a \t to insert tabs between values.

like image 72
Bob Black Avatar answered Jul 01 '26 22:07

Bob Black



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!