【问题标题】:Add spaces to infront of a string?在字符串前面添加空格?
【发布时间】:2012-04-19 12:27:01
【问题描述】:

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

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

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

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

但是空格应该像上面那样固定,而是int spaces_needed的可变长度。

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

【问题讨论】:

    标签: objective-c ios ios4 ios5


    【解决方案1】:

    你可能会喜欢NSStringstringByPaddingToLength:withString:startingAtIndex

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      相关资源
      最近更新 更多