为什么 PadLeft 不起作用? [英] Why is PadLeft not working?

查看:107
本文介绍了为什么 PadLeft 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

但是当我检查它时,foo 不是 "____blah"(开头有 4 个空格),它只是 "blah".为什么 PadLeft 不符合我的预期?

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?

推荐答案

因为 第一个参数是结果字符串的总长度,而不是要填充的字符数.所以在你的例子中你想使用 8 而不是 4.

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.

这篇关于为什么 PadLeft 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆