This should be easy, but I'm having a hard time finding the easiest solution.
I need an NSString
that is equal to another string concatenated with itself a given number of times.
For a better explanation, consider the following python example:
>> original = "abc"
"abc"
>> times = 2
2
>> result = original * times
"abcabc"
Any hints?
EDIT:
I was going to post a solution similar to the one by Mike McMaster's answer, after looking at this implementation from the OmniFrameworks:
// returns a string consisting of 'aLenght' spaces
+ (NSString *)spacesOfLength:(unsigned int)aLength;
{
static NSMutableString *spaces = nil;
static NSLock *spacesLock;
static unsigned int spacesLength;
if (!spaces) {
spaces = [@" " mutableCopy];
spacesLength = [spaces length];
spacesLock = [[NSLock alloc] init];
}
if (spacesLength < aLength) {
[spacesLock lock];
while (spacesLength < aLength) {
[spaces appendString:spaces];
spacesLength += spacesLength;
}
[spacesLock unlock];
}
return [spaces substringToIndex:aLength];
}
Code reproduced from the file:
Frameworks/OmniFoundation/OpenStepExtensions.subproj/NSString-OFExtensions.m
on the OpenExtensions framework from the Omni Frameworks by The Omni Group.