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.
%s is for string %d is for decimal (or int) %c is for character.
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With