Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String formatting in C# to get identical spacing

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.

like image 823
Jonathan Plumb Avatar asked Jul 23 '13 19:07

Jonathan Plumb


People also ask

What is %s and %D in C?

%s is for string %d is for decimal (or int) %c is for character.

What is a string formatting?

String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text. custom_string = "String formatting" print(f"{custom_string} is a powerful technique") Powered by Datacamp Workspace. String formatting is a powerful technique.

What does %d mean in string?

The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.

What does %U mean in C?

An unsigned Integer means the variable can hold only a positive value. This format specifier is used within the printf() function for printing the unsigned integer variables. Syntax: printf(“%u”, variable_name); or.


2 Answers

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.
like image 165
voithos Avatar answered Oct 03 '22 16:10

voithos


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();
like image 35
John Kraft Avatar answered Oct 03 '22 14:10

John Kraft