Why is PadLeft not working?

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-26 23:48:12

问题


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?


回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/3581416/why-is-padleft-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!