Objective-C 代码右填充 NSString?

发布于 2024-10-24 23:00:43 字数 283 浏览 1 评论 0原文

有人可以给出如何在 Objective-C 中正确填充 NSString 的代码示例吗?

例如想要这些字符串:

Testing 123 Long String
Hello World
Short 

如果右填充到列宽为 12:然后在每个字符串的末尾添加一个字符串“XXX”,则会给出:

Testing 123 xxx
Hello World xxx
Short       xxx

这是第二列想要向上。

Can someone give a code example of how to right pad an NSString in objective-c please?

For example want these strings:

Testing 123 Long String
Hello World
Short 

if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give:

Testing 123 xxx
Hello World xxx
Short       xxx

That is a 2nd column would like up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猥︴琐丶欲为 2024-10-31 23:00:43

亚当走在正确的道路上,但还没有完全实现。您确实想使用+stringWithFormat:,但不完全像他建议的那样。如果您想将“someString”填充到(例如)至少 12 个字符,则可以使用宽度说明符作为格式的一部分。由于您希望结果左对齐,因此需要在宽度说明符前加上减号:

NSString *padded = [NSString stringWithFormat:@"%-12@", someString];

或者,如果您希望结果恰好 12 个字符,则可以同时使用最小和最大宽度说明符:

NSString *padded = [NSString stringWithFormat:@"%-12.12@", someString];

Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad "someString" to (say) a minimum of 12 characters, you'd use a width specifier as part of the format. Since you want the result to be left-justified, you need to precede the width specifier with a minus:

NSString *padded = [NSString stringWithFormat:@"%-12@", someString];

Or, if you wanted the result to be exactly 12 characters, you can use both minimum and maximum width specifiers:

NSString *padded = [NSString stringWithFormat:@"%-12.12@", someString];
撧情箌佬 2024-10-31 23:00:43

第二列什么会排列起来?

鉴于您使用的是 iOS,使用 HTML 或表格视图比尝试将字符与空格对齐要简单得多。除了在编程上更加优雅之外,它还会看起来更好,对输入数据随时间的变化更具弹性,并使用户体验更符合平台的期望。

如果您确实想要使用空格,那么您将不得不将您的 UI 限制为等宽字体(出于特定上下文之外的可读性目的,例如源代码,这很丑陋),并且国际字符将被有点痛。

从这里开始,就需要获取字符串长度(请记住,在某些语言中,“长度”不一定意味着“字符数”),使用 substringWithRange 进行一些数学计算: 并向结果附加空格。

2nd column of what would line up?

Given that you are on iOS, using HTML or a table view would be far more straightforward than trying to line up characters with spaces. Beyond being programmatically more elegant, it will look better, be more resilient to input data changes over time, and render a user experience more in line with expectations for the platform.

If you really want to use spaces, then you are going to have to limit your UI to a monospace font (ugly for readability purposes outside of specific contexts, like source code) and international characters are going to be a bit of a pain.

From there, it would be a matter of getting the string length (keeping in mind that "length" does not necessarily mean "# of characters" in some languages), doing a bit of math, using substringWithRange: and appending spaces to the result.

放低过去 2024-10-31 23:00:43

不幸的是,Objective-C 不允许使用 %@ 的格式说明符。填充的解决方法如下:

NSString *padded = [NSString stringWithFormat:@"%@%*s", someString, 12-someString.length, ""];

将字符串向右填充空格,字段长度最多为 12 个字符。

Unfortunately, Objective-C does not allow format specifiers for %@. A work-around for padding is the following:

NSString *padded = [NSString stringWithFormat:@"%@%*s", someString, 12-someString.length, ""];

which will pad the string to the right with spaces up to a field length of 12 characters.

伏妖词 2024-10-31 23:00:43

%-@ 不起作用,但 %-s 起作用

NSString *x = [NSString stringWithFormat:@"%-3@", @"a" ];
NSString *y = [NSString stringWithFormat:@"%-3@", @"abcd" ];
NSString *z = [NSString stringWithFormat:@"%-3@ %@", @"a", @"bc" ];
NSString *zz = [NSString stringWithFormat:@"%-3s %@", "a", @"bc" ];
NSLog(@"[%@][%@][%@][%@].......", x,y,z,zz);

输出:
[a][abcd][a bc][a bc].......

%-@ does not work, but %-s works

NSString *x = [NSString stringWithFormat:@"%-3@", @"a" ];
NSString *y = [NSString stringWithFormat:@"%-3@", @"abcd" ];
NSString *z = [NSString stringWithFormat:@"%-3@ %@", @"a", @"bc" ];
NSString *zz = [NSString stringWithFormat:@"%-3s %@", "a", @"bc" ];
NSLog(@"[%@][%@][%@][%@].......", x,y,z,zz);

output:
[a][abcd][a bc][a bc].......

顾忌 2024-10-31 23:00:43

尝试下面。它对我有用。

NSString *someString = @"1234";
NSString *padded = [someString stringByPaddingToLength: 16 withString: @"x" startingAtIndex:0];

NSLog(@"%@", someString);
NSLog(@"%@", padded);

Try below. its working for me.

NSString *someString = @"1234";
NSString *padded = [someString stringByPaddingToLength: 16 withString: @"x" startingAtIndex:0];

NSLog(@"%@", someString);
NSLog(@"%@", padded);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文