如何反复加入字符串?

时间:2010-07-31 11:52:45

标签: iphone objective-c

我想做类似的事情:

string s1 set to null
s2: "abc"

repeat 10 times

s1=s1+s2

我如何在objective-c中执行此操作?

2 个答案:

答案 0 :(得分:3)

这适合吗?:

NSMutableString *s1 = [[NSMutableString alloc] initWithString:@""];
NSString *s2 = @"abc";

for(NSInteger idx = 0; idx < 10; ++idx) {
   [s1 appendString:s2];
}

...

[s1 release];

答案 1 :(得分:0)

虽然@thatsdisgusting给出了一个完美的答案,但这是一个捷径:

NSMutableString *a = [NSMutableString stringWithCapacity:0];
NSString *pad = @"abc";
NSString *ret = [a stringByPaddingToLength:10*[pad length] withString:pad
                                                           startingAtIndex:0];

滥用stringByPaddingToLength