-1

I want to get a number of space, how can I get that quickly? I don't want to do this by the following code.

NSString *str = @"";
// for example I want to get a string with 10 spaces
for (int i = 0; i< 10; i++) {
    str= [str stringByAppendingString:@" "];
}

I think the code is not an efficient one. who can give me another more efficient way.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
bohan
  • 1,659
  • 3
  • 17
  • 29

2 Answers2

3
[@"" stringByPaddingToLength:10 withString:@" " startingAtIndex:0]
Denis Mikhaylov
  • 2,035
  • 21
  • 24
0

You could do this:

[[string componentsSeparatedByString:@" "] count]

Also, see this question for a couple of other solutions: Number of occurrences of a substring in an NSString?

Community
  • 1
  • 1