我想在字符串前面添加空格 ("")(如空格)。这是空格的长度
int spaces_needed=((36-[[dicBusinessDetails objectForKey:@"ResturantName"] length]))/2;
我想要的是创建一个这样的字符串
NSString stringWithFormat:@" %@",[dicBusinessDetails objectForKey:@"ResturantName"];
但空格应该不是如上固定,而是可变长度的int spaces_needed。
这样做的实际原因是将收据中的字符串居中对齐
最佳答案
你可能喜欢 NSString
的 stringByPaddingToLength:withString:startingAtIndex
NSString *result = [NSString stringWithFormat:@"%@%@",
[@" " stringByPaddingToLength:spaces_needed withString:@" " startingAtIndex:0]
[dicBusinessDetails objectForKey:@"ResturantName"]];
关于objective-c - 在字符串前面添加空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10228052/