5

I've been looking up string formatting and frankly I'm getting confused. This is what I want to do.

I have a "character stats" page (this is a console app), and I want it formatted like this:

=----------------------------------=
= Strength: 24     | Agility: 30   =
= Dexterity: 30    | Stamina: 28   =
= Magic: 12        | Luck:    18   =
=----------------------------------=

I guess basically I'm trying to find out how to make that middle '|' divider be in the same place regardless of how many letters the stat is or how many points the stat is.

Thanks for the input.

Edit: I also want the ending '=' to also be in the same spot.

Jonathan Plumb
  • 417
  • 1
  • 4
  • 12

4 Answers4

12

I learned something new, it seems! As some of the others have mentioned, you can accomplish the same thing using String.Format.

The interpolation strings used in String.Format can also include an optional alignment component.

//                index   alignment
//                    v   v
String.Format("Hello {0,-10}!", "World");

When this is negative, then the string is left-aligned. When positive, it is right aligned. In both cases, the string is padded correspondingly with whitespace if it is shorter than the specified width (otherwise, the string is just inserted fully).

I believe this is an easier and more readable technique than having to fiddle with String.PadRight.


You can also use String.PadRight (or String.PadLeft). Example:

class Stats {
    // Contains properties as you defined ...
}

var stats = new Stats(...);

int leftColWidth = 16;
int rightColWidth = 13;

var sb = new StringBuilder();
sb.AppendLine("=----------------------------------=");
sb.Append("= ");
sb.Append(("Strength: " + stats.Strength.ToString()).PadRight(leftColWidth));
sb.Append(" | ");
sb.Append(("Agility: " + stats.Agility.ToString()).PadRight(rightColWidth));
// And so on.
voithos
  • 68,482
  • 12
  • 101
  • 116
  • I didn't even know that "Pad" existed. I like it! Thanks!! – Jonathan Plumb Jul 23 '13 at 19:11
  • 1
    @JonathanPlumb: *I* didn't know that alignment existed in `String.Format`. Updated answer. – voithos Jul 23 '13 at 19:21
  • 1
    You can even write your own custom string format providers to do anything you want inside a string format call with custom implimentations of `IFormatProvider` and `ICustomFormatter` – asawyer Jul 23 '13 at 19:37
8

I used to use this technique a lot back in the 80's doing text based games. Obviously we didn't have string.Format back in those days; but it allows you to visualize the layout in the code.

Pre-format the text as you want it to be laid out, then just use the string.Format() function like so...

            string formattedText = @"
=----------------------------------=
= Strength:  {0,2}   | Agility: {3,2}    =
= Dexterity: {1,2}   | Stamina: {4,2}    =
= Magic:     {2,2}   | Luck:    {5,2}    =
=----------------------------------=".Trim();
            string output = string.Format(formattedText, 12, 13, 14, 15, 16, 1);
            Console.WriteLine(output);
            Console.ReadLine();
John Kraft
  • 6,811
  • 4
  • 37
  • 53
5
String.Format("{0,-20}|","Dexterity: 30") 

would align the value to the left and pad it to 20 characters. The only problem is that if the parameter is longer than 20 it would not be truncated.

1

You will need to use a String.PadRight or a String.PadLeft. Do something like this:

Trip_Name1 = Trip_Name1.PadRight(20,' ');

This is what you are looking for I think.

Dozer789
  • 1,980
  • 2
  • 23
  • 43