0

I want to do something like:

string s1 set to null
s2: "abc"

repeat 10 times

s1=s1+s2

How can I do this in objective-c?

2 Answers 2

3

Is this suitable?:

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

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

...

[s1 release];
Sign up to request clarification or add additional context in comments.

2 Comments

+1. But in spirit of obj-c memory management rules and the bugs we see here so often, maybe better call [NSMutableString stringWithString:@""] to get an autoreleased string.
Or just [NSMutableString string].
0

Although @thatsdisgusting gave a perfect answer, here's a shortcut:

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

abusing stringByPaddingToLength.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.