I'm trying to add a character many times before a string. AMAIK in C#, it's PadLeft.
string firstName = "Mary";
firstName = firstName.PadLeft(3, '*'); // This should return ***Mary
But it doesn't work. Am I doing something wrong?
The first argument is total length of the returned string, as "Mary" is 4 characters long and your first argument is 3, it is working as expected. If you try firstName.PadLeft(6, '*')
you'll get **Mary.
You should add length of your string like that:
firstName = firstName.PadLeft(firstName.Length + 3, '*');
First parameter(totalWidth) represents the result string length. If your string length is less then totalWidth parameter, PadLeft adds so many chars that result string length will be equal to totalWidth.
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