NSString 中有 setwidth 吗?
我想在字符串后面附加固定宽度的空格;例如,我想以这种格式显示数据:
1 faizan 1 locked 10/20/2008
1 faizan 1 locked 10/20/2008
1 faizan 1 locked 10/20/2008
1 faizan 1 locked 10/20/2008
但这仅适用于相同长度的字符串。如果字符串像这样不同:
1 faizan 1 locked 10/20/2008
2 faizanali 1 locked 10/20/2008
3 ali turab gilani 1 locked 10/20/2008
那么这不会修复空格。为了让我可以用相同数量的空格显示数据,我尝试了以下方法:
NSString *titleColumn = [[NSString stringWithFormat:@"%@:", title] stringByPaddingToLength:20 withString:@" " startingAtIndex:0];
serializedValue = [serializedValue stringByAppendingFormat:@"%@%@", titleColumn, value];
但这也不起作用。 Objective-C 是否有像 C++ 和其他语言中那样的 setwidth
类型函数?
I want to append my string with fixed-width spaces; for example, I want to show the data in this format:
1 faizan 1 locked 10/20/2008
1 faizan 1 locked 10/20/2008
1 faizan 1 locked 10/20/2008
1 faizan 1 locked 10/20/2008
But this only works with strings of the same length. If the strings are different like this:
1 faizan 1 locked 10/20/2008
2 faizanali 1 locked 10/20/2008
3 ali turab gilani 1 locked 10/20/2008
then this doesn't fix the spaces. So that I can show the data in with the same number of spaces, I tried this:
NSString *titleColumn = [[NSString stringWithFormat:@"%@:", title] stringByPaddingToLength:20 withString:@" " startingAtIndex:0];
serializedValue = [serializedValue stringByAppendingFormat:@"%@%@", titleColumn, value];
But this also didn't work. Does Objective-C have a setwidth
-type function like in C++ and in other languages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是这样做的吗?
/* 定义辅助方法 */
/* 使用辅助方法 */
/* 以及输出 */
Are you doing it in this way?
/* Define a helper method */
/* Use the helper method */
/* And the output */
您必须使用 NSMutableString 来实现这一点。逻辑是:
maxLength
。maxLength
,则在字符串后附加“”(空格)。You have to use
NSMutableString
to implement this. The logic is:maxLength
.maxLength
.