创buildNSString重复另一个string给定的次数

这应该很容易,但我很难find最简单的解决scheme。

我需要一个NSString ,它等于另一个string连接自己给定的次数。

为了更好的解释,请考虑下面的python示例:

 >> original = "abc" "abc" >> times = 2 2 >> result = original * times "abcabc" 

任何提示?


编辑:

从OmniFrameworks看这个实现之后,我打算发布一个与Mike McMaster的答案类似的解决scheme:

 // 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]; } 

代码从文件中转载:

 Frameworks/OmniFoundation/OpenStepExtensions.subproj/NSString-OFExtensions.m 

来自Omni Group的Omni Frameworks的OpenExtensions框架。

有一个叫做stringByPaddingToLength:withString:startingAtIndex:的方法stringByPaddingToLength:withString:startingAtIndex: ::

 [@"" stringByPaddingToLength:100 withString: @"abc" startingAtIndex:0] 

请注意,如果你想要3个ABC,比使用9( 3 * [@"abc" length] )或创build类别如下:

 @interface NSString (Repeat) - (NSString *)repeatTimes:(NSUInteger)times; @end @implementation NSString (Repeat) - (NSString *)repeatTimes:(NSUInteger)times { return [@"" stringByPaddingToLength:times * [self length] withString:self startingAtIndex:0]; } @end 
 NSString *original = @"abc"; int times = 2; // Capacity does not limit the length, it's just an initial capacity NSMutableString *result = [NSMutableString stringWithCapacity:[original length] * times]; int i; for (i = 0; i < times; i++) [result appendString:original]; NSLog(@"result: %@", result); // prints "abcabc" 

为了performance,你可以用类似这样的东西进入C:

 + (NSString*)stringWithRepeatCharacter:(char)character times:(unsigned int)repetitions; { char repeatString[repetitions + 1]; memset(repeatString, character, repetitions); // Set terminating null repeatString[repetitions] = 0; return [NSString stringWithCString:repeatString]; } 

这可以写成NSString类的类别扩展。 这里可能有一些检查,但这是它的直接要点。

上面的第一种方法是针对单个字符。 这是一个string。 它也可以用于单个字符,但有更多的开销。

 + (NSString*)stringWithRepeatString:(char*)characters times:(unsigned int)repetitions; { unsigned int stringLength = strlen(characters); unsigned int repeatStringLength = stringLength * repetitions + 1; char repeatString[repeatStringLength]; for (unsigned int i = 0; i < repetitions; i++) { unsigned int pointerPosition = i * repetitions; memcpy(repeatString + pointerPosition, characters, stringLength); } // Set terminating null repeatString[repeatStringLength - 1] = 0; return [NSString stringWithCString:repeatString]; } 

如果你在Python中使用Cocoa,那么你可以这么做,因为PyObjC使NSString具有所有的Python unicode类的能力。

否则,有两种方法。

一个是用n相同的string创build一个数组,并使用componentsJoinedByString: 像这样的东西:

 NSMutableArray *repetitions = [NSMutableArray arrayWithCapacity:n]; for (NSUInteger i = 0UL; i < n; ++i) [repetitions addObject:inputString]; outputString = [repetitions componentsJoinedByString:@""]; 

另一种方法是从一个空的NSMutableString开始,并将string追加到n次,如下所示:

 NSMutableString *temp = [NSMutableString stringWithCapacity:[inputString length] * n]; for (NSUInteger i = 0UL; i < n; ++i) [temp appendString:inputString]; outputString = [NSString stringWithString:temp]; 

如果你可以在这里返回一个可变的string,你可以切断stringWithString: call。 否则,你可能应该返回一个不可变的string,这里的stringWithString: message意味着你在内存中有两个string的副本。

因此,我build议使用componentsJoinedByString:解决scheme。

[编辑:借用的想法使用…WithCapacity: 斯特的答案方法]