String format in C#

Posted on 2008-01-29
Medium Priority
Last Modified: 2008-06-12

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.

Question by:Jetixil
  • 3
LVL 11

Expert Comment

ID: 20772901
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.
LVL 22

Expert Comment

ID: 20774179
This is an exceptionally popular page on string formatting in c#:

Look for the term 'alignment' to learn how to do right alignment.

Accepted Solution

jarw earned 2000 total points
ID: 20775583
The code sample below might get you going in the right direction. The part to focus on is that the "-10" is telling it to format the first string such that it allows room for 10 characters and the text should be left aligned.
/* The output of this code is:
1 2 3 4   Testing...
1 2 3     Testing...
1 2       Testing...
1         Testing...
Console.WriteLine("{0,-10}{1}", "1 2 3 4", "Testing...");
Console.WriteLine("{0,-10}{1}", "1 2 3", "Testing...");
Console.WriteLine("{0,-10}{1}", "1 2", "Testing...");
Console.WriteLine("{0,-10}{1}", "1", "Testing...");

Open in new window

LVL 11

Expert Comment

ID: 20845426
Jetixil, jarw gave you a very good example.  Did it answer your question?
LVL 11

Expert Comment

ID: 21739430
My opinion is that the answer should be awarded to jarw.  He fully answered the original poster's question with a working example.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses
Course of the Month3 days, 9 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question