objective-c - 在字符串前面添加空格?

标签 objective-c ios ios4 ios5

我想在字符串前面添加空格 ("")(如空格)。这是空格的长度

 int spaces_needed=((36-[[dicBusinessDetails objectForKey:@"ResturantName"] length]))/2;

我想要的是创建一个这样的字符串

 NSString stringWithFormat:@"               %@",[dicBusinessDetails objectForKey:@"ResturantName"];

但空格应该不是如上固定,而是可变长度的int spaces_needed

这样做的实际原因是将收据中的字符串居中对齐

最佳答案

你可能喜欢 NSStringstringByPaddingToLength:withString:startingAtIndex

NSString *result = [NSString stringWithFormat:@"%@%@",
                             [@" " stringByPaddingToLength:spaces_needed withString:@" " startingAtIndex:0]
                             [dicBusinessDetails objectForKey:@"ResturantName"]];

关于objective-c - 在字符串前面添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10228052/

相关文章:

iphone - 使用不带 alertView 的 UIWebView 在 iOS 中以编程方式调用电话

ios - 带有特殊字符的查询不获取数据( Objective-C )

iphone - 在 objective-c 中将 NSString 转换为 Integer

ios - 使用 UISegmentControl 切换 View 时 UISearchBar 消失

ios - 切换音乐应用后应用不再接收来自锁屏的控制事件

iphone - 如何编译 xcode4 项目以用作第二个项目的框架?

ios - 寻找触手可及的通用蓝牙设备

ios - UIAlertControllerAnimatedTransitioning 内部崩溃

ios - 如何在单元格中启用 Google map 按钮?

ipad - Iphone sdk 在没有整个上下文的情况下保存图纸