C
C#15mo 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_S15mo 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
Heisenberg15mo ago
oh how can i fix that?
Chiyoko_S
Chiyoko_S15mo ago
so padding is the amount of new characters to add on each side, right?
Heisenberg
Heisenberg15mo ago
yea
Chiyoko_S
Chiyoko_S15mo 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
Heisenberg15mo ago
what does Hello.Length + padding and str2.Length + padding does?
Chiyoko_S
Chiyoko_S15mo 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
Heisenberg15mo ago
wait u can use leftPadded() as first parameters of PadRight()?
Chiyoko_S
Chiyoko_S15mo ago
why not oh
Heisenberg
Heisenberg15mo ago
isnt first parameter int? oh
Chiyoko_S
Chiyoko_S15mo ago
this is why you don't code without ide
Heisenberg
Heisenberg15mo ago
nvm
Chiyoko_S
Chiyoko_S15mo ago
grinowo I think you got the idea
Heisenberg
Heisenberg15mo ago
its just an console app XD ig
Chiyoko_S
Chiyoko_S15mo ago
though as I said above You do need to consider this
Heisenberg
Heisenberg15mo ago
i kinda think it may give same output as last time?
ffmpeg -i me -f null -
second padding would be of the whole length of the console
Heisenberg
Heisenberg15mo ago
?
ffmpeg -i me -f null -
what's the doubt apart that probably i wouldn't use padding for this just add characters
Heisenberg
Heisenberg15mo 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_S15mo ago
Chiyoko_S
Chiyoko_S15mo 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
Heisenberg15mo 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_S15mo ago
you have Hello.Length in both the PadLeft and PadRight calls
ffmpeg -i me -f null -
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
Heisenberg15mo ago
output
Chiyoko_S
Chiyoko_S15mo ago
the second call should have Hello_Mod.Length
Heisenberg
Heisenberg15mo ago
what does it do? oh
Chiyoko_S
Chiyoko_S15mo ago
it gives you a '#' character repeated padding times but that's more allocation!
Heisenberg
Heisenberg15mo ago
OHH
Heisenberg
Heisenberg15mo ago
Thank you so much <3 Ahh can i ask something?
Chiyoko_S
Chiyoko_S15mo ago
sure?
Heisenberg
Heisenberg15mo 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_S15mo 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
ffmpeg -i me -f null -
we could say that pad left goes from 0 to console center, and pad right goes from center to console width
Chiyoko_S
Chiyoko_S15mo 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
Heisenberg15mo ago
oh
Accord
Accord14mo 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.