C
C#2y ago
Heisenberg

❔ Padding string from two sides

im trying to do padding of string from both side but it doesnt work. The Code:
string Hello = " Hello, World ";
int padding = (Console.WindowWidth - Hello.Length) / 2;
string Hello_Mod = Hello.PadLeft(padding,'#');
string Hello_Modd = Hello_Mod.PadRight(padding,'#');
Console.WriteLine(Hello_Modd);
Console.WriteLine(padding);
Console.WriteLine(Console.WindowWidth);
Console.WriteLine(Hello.Length);
Console.WriteLine(Console.WindowWidth / 2);
string Hello = " Hello, World ";
int padding = (Console.WindowWidth - Hello.Length) / 2;
string Hello_Mod = Hello.PadLeft(padding,'#');
string Hello_Modd = Hello_Mod.PadRight(padding,'#');
Console.WriteLine(Hello_Modd);
Console.WriteLine(padding);
Console.WriteLine(Console.WindowWidth);
Console.WriteLine(Hello.Length);
Console.WriteLine(Console.WindowWidth / 2);
The ouput im getting:
#################### Hello, World
#################### Hello, World
The output i wanted
#################### Hello, World ####################
#################### Hello, World ####################
38 Replies
Chiyoko_S
Chiyoko_S2y ago
PadLeft and PadRight pads to the specified length that is it adds # until length of padding is reached if you do str.PadLeft(len, "#") then str2.PadRight(len, "#") does nothing because length of len is already reached in the first PadLeft call
Heisenberg
HeisenbergOP2y ago
oh how can i fix that?
Chiyoko_S
Chiyoko_S2y ago
so padding is the amount of new characters to add on each side, right?
Heisenberg
HeisenbergOP2y ago
yea
Chiyoko_S
Chiyoko_S2y ago
do Hello.Length + padding in the first call, then do str2.Length + padding? in the first one you add padding amount on the left side in the second one you add another padding amount on the right side Though I'd just avoid using PadLeft and PadRight altogether
Heisenberg
HeisenbergOP2y ago
what does Hello.Length + padding and str2.Length + padding does?
Chiyoko_S
Chiyoko_S2y ago
let's say you have str as the original string and you get the amount of new characters to add on each side by doing (width - length) / 2, let's call that padding (by the way, that wouldn't quite work with odd-number length strings) var leftPadded = str.PadLeft(str.Length + padding, "#") would be the original string + padding amount of #s added on the left side as I said above: the length you pass is the final length after padding same goes for right padding var padded = leftPadded.PadRight(leftPadded.Length + padding, "#") adds another padding amount of padding characters on the left padded string
Heisenberg
HeisenbergOP2y ago
wait u can use leftPadded() as first parameters of PadRight()?
Chiyoko_S
Chiyoko_S2y ago
why not oh
Heisenberg
HeisenbergOP2y ago
isnt first parameter int? oh
Chiyoko_S
Chiyoko_S2y ago
this is why you don't code without ide
Heisenberg
HeisenbergOP2y ago
nvm
Chiyoko_S
Chiyoko_S2y ago
grinowo I think you got the idea
Heisenberg
HeisenbergOP2y ago
its just an console app XD ig
Chiyoko_S
Chiyoko_S2y ago
though as I said above You do need to consider this
Heisenberg
HeisenbergOP2y ago
i kinda think it may give same output as last time?
Omnissiah
Omnissiah2y ago
second padding would be of the whole length of the console
Heisenberg
HeisenbergOP2y ago
?
Omnissiah
Omnissiah2y ago
what's the doubt apart that probably i wouldn't use padding for this just add characters
Heisenberg
HeisenbergOP2y ago
Code i gave automatically find amount to chars to pad from both side to center main string but it only pads from one side
Chiyoko_S
Chiyoko_S2y ago
Chiyoko_S
Chiyoko_S2y ago
seems to work fine but as I said this gives a string that's 39 characters in total you'd need to add an extra character on either left or right if the string length is odd-numbered what's the code you currently have? the full code
Heisenberg
HeisenbergOP2y ago
using System;

class Program{
static void Main(){
string Hello = " Hello, World ";
int padding = (Console.WindowWidth - Hello.Length) / 2;
string Hello_Mod = Hello.PadLeft(Hello.Length + padding,'#');
string Hello_Modd = Hello_Mod.PadRight(Hello.Length + padding,'#');
Console.WriteLine(Hello_Modd);
Console.WriteLine(padding);
Console.WriteLine(Console.WindowWidth);
Console.WriteLine(Hello.Length);
Console.WriteLine(Console.WindowWidth / 2);
}
}
using System;

class Program{
static void Main(){
string Hello = " Hello, World ";
int padding = (Console.WindowWidth - Hello.Length) / 2;
string Hello_Mod = Hello.PadLeft(Hello.Length + padding,'#');
string Hello_Modd = Hello_Mod.PadRight(Hello.Length + padding,'#');
Console.WriteLine(Hello_Modd);
Console.WriteLine(padding);
Console.WriteLine(Console.WindowWidth);
Console.WriteLine(Hello.Length);
Console.WriteLine(Console.WindowWidth / 2);
}
}
Chiyoko_S
Chiyoko_S2y ago
you have Hello.Length in both the PadLeft and PadRight calls
Omnissiah
Omnissiah2y ago
you have already done the work yourself where by work i mean calculating the length of the padding you can just avoid calling pad and call instead new string('#', padding) that's it
Heisenberg
HeisenbergOP2y ago
output
Chiyoko_S
Chiyoko_S2y ago
the second call should have Hello_Mod.Length
Heisenberg
HeisenbergOP2y ago
what does it do? oh
Chiyoko_S
Chiyoko_S2y ago
it gives you a '#' character repeated padding times but that's more allocation!
Heisenberg
HeisenbergOP2y ago
OHH
Heisenberg
HeisenbergOP2y ago
Thank you so much <3 Ahh can i ask something?
Chiyoko_S
Chiyoko_S2y ago
sure?
Heisenberg
HeisenbergOP2y ago
string Hello_Modd = Hello_Mod.PadRight(Hello_Mod.Length + padding,'#');
string Hello_Modd = Hello_Mod.PadRight(Hello_Mod.Length + padding,'#');
about this line how actually this help?
Chiyoko_S
Chiyoko_S2y ago
basically using my example with the result string being ##################aaa$$$$$$$$$$$$$$$$$$ the first PadLeft gives you ##################aaa which in this case would correspond to your Hello_Mod variable and we want to add another padding on the right side padding times, to that Hello_Mod variable
Omnissiah
Omnissiah2y ago
we could say that pad left goes from 0 to console center, and pad right goes from center to console width
Chiyoko_S
Chiyoko_S2y ago
so that means we want the resulting string to be Hello_Mod.Length + padding characters long you did Hello.Length + padding originally which has no effect because Hello_Mod is already made to be at that length
Heisenberg
HeisenbergOP2y ago
oh
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server