String formatting in C# to get identical spacing -
i've been looking string formatting , frankly i'm getting confused. want do.
i have "character stats" page (this console app), , want formatted this:
=----------------------------------= = strength: 24 | agility: 30 = = dexterity: 30 | stamina: 28 = = magic: 12 | luck: 18 = =----------------------------------=
i guess i'm trying find out how make middle '|' divider in same place regardless of how many letters stat or how many points stat is.
thanks input.
edit: want ending '=' in same spot.
i learned new, seems! of others have mentioned, can accomplish same thing using string.format
.
the interpolation strings used in string.format
can include optional alignment component.
// index alignment // v v string.format("hello {0,-10}!", "world");
when negative, string left-aligned. when positive, right aligned. in both cases, string padded correspondingly whitespace if shorter specified width (otherwise, string inserted fully).
i believe easier , more readable technique having fiddle string.padright
.
you can use string.padright
(or string.padleft
). example:
class stats { // contains properties 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)); // , on.
Comments
Post a Comment