Link to home
Start Free TrialLog in
Avatar of Jetixil
Jetixil

asked on

String format in C#

Hi,

im having a problem , i have made some application which should write like this this:

1 4 4 2 5     2  dice are dead this roll. The score for this roll is 9
1 4 2          1 die is dead this roll. The score for this roll is 5
2 2             2 dice are dead this roll. The score for this roll is 0

i have written Console.Write(" Number of dead dice in this role: " + deadDice + "."); but now i have a problem because it writes it like this

 1 4 4 2 5     2 dice are dead this roll. The score for this roll is 9
1 4 2         1 die is dead this roll. The score for this roll is 5
2 2           2 dice are dead this roll. The score for this roll is 0

i have to make the "X dice are dead this roll......" to be under the last line exactly but it moves every time that a num is deleted.   can you please explain me how can i do it?  thanks.


Avatar of Expert1701
Expert1701

You can use the String.PadLeft and String.PadRight methods to format your output.  If you can list your code where it prints the "1 4 4 2 5     2  dice are dead this roll. The score for this roll is 9", I will show you how to adjust it.
This is an exceptionally popular page on string formatting in c#:
http://blog.stevex.net/index.php/string-formatting-in-csharp/

Look for the term 'alignment' to learn how to do right alignment.
ASKER CERTIFIED SOLUTION
Avatar of jarw
jarw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jetixil, jarw gave you a very good example.  Did it answer your question?
My opinion is that the answer should be awarded to jarw.  He fully answered the original poster's question with a working example.