目标c-通过重复给定次数另一个字符串来创建NSString

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

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

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

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

有什么提示吗?


编辑:

在查看了OmniFrameworks的实现之后,我打算发布一种类似于Mike McMaster的答案的解决方案:

// 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框架的信息。

Sergio Acosta asked 2019-11-09T03:52:32Z
5个解决方案
159 votes

有一种名为3 * [@"abc" length]096的方法:

[@"" 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
tig answered 2019-11-09T03:52:54Z
7 votes
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"
Mike McMaster answered 2019-11-09T03:53:11Z
4 votes

为了提高性能,您可以使用以下代码进入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类的类别扩展。 可能有一些检查应该扔在那里,但这是直截了当的要点。

answered 2019-11-09T03:53:43Z
2 votes

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

+ (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];
}
answered 2019-11-09T03:54:07Z
1 votes

如果您在Python中使用Cocoa,则可以这样做,因为PyObjC将NSString与Python …WithCapacity:类的所有功能融合在一起。

否则,有两种方法。

一种是使用…WithCapacity:创建一个具有相同字符串的数组,并使用stringWithString:。如下所示:

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

另一种方法是从一个空的…WithCapacity:开始并将字符串附加到它stringWithString:次,如下所示:

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

如果可以在此处返回可变字符串,则可以取消…WithCapacity:调用。 否则,您可能应该返回一个不变的字符串,并且此处的stringWithString:消息表示您在内存中拥有该字符串的两个副本。

因此,我建议…WithCapacity:解决方案。

[编辑:借用Mike McMaster的答案中的…WithCapacity:的想法。]

Peter Hosey answered 2019-11-09T03:55:13Z
translate from https://stackoverflow.com:/questions/260945/create-nsstring-by-repeating-another-string-a-given-number-of-times