通过重复给定次数的另一个字符串来创建NSString

时间:2008-11-04 05:03:52

标签: objective-c cocoa string

这应该很容易,但我很难找到最简单的解决方案。

我需要一个NSString等于另一个与自身连接一定次数的字符串。

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

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

任何提示?


编辑:

在从OmniFrameworks查看此实现之后,我将发布类似于Mike McMaster's answer的解决方案:

// 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 Frameworks The Omni Group的OpenExtensions框架。

5 个答案:

答案 0 :(得分:157)

有一种名为stringByPaddingToLength:withString:startingAtIndex:的方法:

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

请注意,如果你想要3个abc,而不是使用9个(3 * [@"abc" length])或者像这样创建类别:

@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

答案 1 :(得分:7)

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"

答案 2 :(得分:4)

对于性能,您可以使用以下内容进入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类的类别扩展。可能会有一些检查应该放在那里,但这是它的直接要点。

答案 3 :(得分:2)

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

+ (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];
}

答案 4 :(得分:1)

如果您在Python中使用Cocoa,那么您可以这样做,因为PyObjC将NSString赋予所有Python unicode类的能力。

否则,有两种方式。

一种是在n次创建一个具有相同字符串的数组,并使用componentsJoinedByString:。像这样:

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

另一种方法是从空NSMutableString开始,然后将字符串附加到n次,如下所示:

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

如果可以在此处返回可变字符串,则可以删除stringWithString:调用。否则,你可能应该返回一个不可变的字符串,这里的stringWithString:消息意味着你在内存中有两个字符串副本。

因此,我建议使用componentsJoinedByString:解决方案。

[编辑:借用Mike McMaster's answer中使用…WithCapacity:方法的想法。]