Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PadLeft not working?

I have the following code:

string foo = "blah".PadLeft(4, ' ');

But when I check it, instead of foo being "____blah" (with 4 spaces at the start), it's just "blah". Why doesn't PadLeft do what I expect?

like image 735
RCIX Avatar asked Aug 27 '10 04:08

RCIX


2 Answers

The first argument to PadLeft specifies the total resulting length you want. Since "blah" starts out four characters long, no padding is added when you specify a length of four. If you want to insert four spaces, change your call to string foo = "blah".PadLft(8, ' '); Since the default padding character is a space, you can skip specifying that and just use string foo = "blah".PadLeft(8)

like image 126
Jerry Coffin Avatar answered Oct 22 '22 11:10

Jerry Coffin


Because the first argument is total length of resulting string and not number of character to pad. So in your example you want to use 8 instead of 4.

like image 21
Brian Rasmussen Avatar answered Oct 22 '22 12:10

Brian Rasmussen