C
C#2y ago
Rype

String alignment

I want to align the string "Set" so it doesn't get affected by the length change in the parentheses. Doing {1,4} or {2,4} won't solve it because the right parenthesis spoils it. Console.WriteLine("A: ({0},{1}) Set {2} Health:{3,-3}", AX, AY, setA, hpA); Console.WriteLine("B: ({0},{1}) Set {2} Health:{3,-3}", BX, BY, setB, hpB); Console.WriteLine("C: ({0},{1}) Set {2} Health:{3,-3}", CX ,CY, setC, hpC); PS - I want it to look like: A: (-1,-5) Set 2 Health:80
B: (7,8) Set 1 Health:60 C: (-6,-4) Set 3 Health:100
11 Replies
qqdev
qqdev2y ago
What does it currently look like?
Tvde1
Tvde12y ago
You can use string.PadLeft
Rype
Rype2y ago
A: (6,-3) Set 3 Health:80 B: (4,4) Set 1 Health:60 C: (-5,-9) Set 2 Health:100 i kinda wanna do it in one line of code
qqdev
qqdev2y ago
Not related but Health:{3,-3} can be just Health:{3} I guess I don't think you can fix your issue in a short way You could preserve some space for the coords but that would result in a different output (more whitespace)
Rype
Rype2y ago
adding "" as a new variable and using - can fix it but i wish there were a simpler way. it just doesnt feel right
x0rld
x0rld2y ago
you mean this " " ? 🤔
Rype
Rype2y ago
will try both now 🙂 it won't work - i think i have to align left all of this A: ({0},{1}) but cant do it
ero
ero2y ago
use tuples and string interpolation
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
var tuple = (-1, -5);
var set = 2;
var hp = 80;

Console.WriteLine($"A: {tuple,-9} Set {set,-2} Health: {hp,3}");
var tuple = (-1, -5);
var set = 2;
var hp = 80;

Console.WriteLine($"A: {tuple,-9} Set {set,-2} Health: {hp,3}");
Console Output
A: (-1, -5) Set 2 Health: 80
A: (-1, -5) Set 2 Health: 80
Compile: 670.099ms | Execution: 39.289ms | React with ❌ to remove this embed.
Rype
Rype2y ago
thank you all edit: i was able to do it in one line. Console.WriteLine("A: {0,-7} Set {1,-2} Health:{2}", String.Format("({0},{1})", AX, AY), setA, hpA); Console.WriteLine("B: {0,-7} Set {1,-2} Health:{2}", String.Format("({0},{1})", BX, BY), setB, hpB); Console.WriteLine("C: {0,-7} Set {1,-2} Health:{2}", String.Format("({0},{1})", CX, CY), setC, hpC);
qqdev
qqdev2y ago
Nice! Yeah that's what I meant with reserving space 👍