6

Ok so basically I want something like

Console.WriteLine(
    "{0}:   {1}/{2}hp    {3}/{4}mp   {5}", 
    character.Identifier, 
    character.CurrentHealth, 
    character.MaxHealth,
    character.CurrentMagic, 
    character.MaxMagic, 
    character.Fatigue
    );

and then have the character.Identifier (which is basically a name) have a set number of letters which it will replace with spaces if needed so that in might print

Josh:   20/20hp    20/20mp   3

or

J:      20/20hp    20/20mp   3

but the HP and then mp and everything else is always in line. I am aware that its probably one of the {0:} but i couldn't figure out one that works

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
Dan
  • 89
  • 2
  • 7
  • 1
    http://msdn.microsoft.com/en-gb/library/txafckwd.aspx <-- MSDN guide to using .NET Formatting based functions (String.Format, Console.WriteLine, etc.) – VisualMelon Jan 06 '13 at 22:41
  • did you tried ***String.Format*** ? _not marked answer_ ***yet*** – Kiquenet Jul 06 '16 at 09:11

3 Answers3

7

The second argument in the {0} notation can give you a fixed width field, so if you wanted the identifier (name) to always be 10 characters long and be left justified, you would use {0,-10}

MSDN is a good resource for this kind of question too, if you read the documentation for String.Format it has an example of a fixed width table that might be similar to what you want.

Also, as Hogan's answer correctly points out, you would have to append the : to the string outside of the format string if you want it right next to the name.

WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
  • that didn't work :S Console.WriteLine("{0,15}: {1}/{2}hp {3}/{4}mp {5}", character.Identifier, character.CurrentHealth, character.MaxHealth, character.CurrentMagic, character.MaxMagic, character.Fatigue); Console.WriteLine("********************************"); – Dan Jan 06 '13 at 22:43
  • @bed - the issue is the `:` -- see my answer. Also, you left justify with a negative number not right justify it. – Hogan Jan 06 '13 at 22:51
5

You can right pad a string with spaces by using:

character.Identifier.PadRight(10);

This should give you the format you are after.

Craig T
  • 2,761
  • 5
  • 25
  • 33
2

I believe this will do what you want:

const int colWidth = 10;
Console.WriteLine("{0,-"+colWidth.ToString()+"}{1,-"+colWidth.ToString()+"}{2,-"+colWidth.ToString()+"}{3}", 
    (character.Identifier+":").PadRight(colWidth+1).Remove(0,colWidth),
    (character.CurrentHealth+"/"+character.MaxHealth+"hp").PadRight(colWidth+1).Remove(0,colWidth),
    (character.CurrentMagic+"/"+character.MaxMagic+"mp").PadRight(colWidth+1).Remove(0,colWidth),
    (character.Fatigue,colWidth));

This will add spaces to the end of string and then truncate the result.
See the docs for String.Format

NOTES

I append the : to the name outside of the format string and I "merge" the hp and mp sections and then put them in a column.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • Have you ever test with the name longer than length of format specified, like "**Josh0123456789**" ? – Ken Kin Jan 07 '13 at 05:37
  • @KenKin - I don't have to test, I know what format does with a specifier like `{0,-10}`, it will truncate it. – Hogan Jan 07 '13 at 06:22
  • Actually format like `{0,-10}` won't do truncate. I don't consider right-aligned as bug. In fact, I tested both alignment, and I believe anchor the colons in a fixed position is reasonable. – Ken Kin Jan 07 '13 at 06:38
  • @KenKin - good point -- fixed that and added a col width constant. – Hogan Jan 07 '13 at 18:21