如何用空格填充 NSString?
例如,我需要 NSString 至少有 8 个字符......而不是使用循环在其上添加左侧填充空格,有没有办法做到这一点?
Examples:
Input: |Output:
Hello | Hello
Bye | Bye
Very Long |Very Long
abc | abc
For example, I need the NSString have at least 8 chars....instead of using a loop to add the left pad spaces on this, is there anyway to do it?
Examples:
Input: |Output:
Hello | Hello
Bye | Bye
Very Long |Very Long
abc | abc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何执行此操作的示例:
Here is an example of how you can do it:
我只是做了这样的事情:
此外,
14
是你想要的宽度。I just do something like this:
Moreover,
14
is the width that you want.您可以通过
-[NSMutableStringappendFormat:]
和所有其他NSString
“格式”方法使用 C 语言 printf 格式化。它不尊重 NSString(对%@
进行格式化),因此您需要将它们转换为 ASCII。C 中的字符串填充
You can use C language printf formatting with
-[NSMutableString appendFormat:]
and all otherNSString
"format" methods. It doesn't respect NSString (do formatting on%@
), so you need to convert them to ASCII.String Padding in C