Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create NSString by repeating another string a given number of times

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.

like image 687
Sergio Acosta Avatar asked Nov 04 '08 05:11

Sergio Acosta


People also ask

How do you repeat a string?

JavaScript String repeat() The repeat() method returns a string with a number of copies of a string. The repeat() method returns a new string. The repeat() method does not change the original string.

How do you replicate a string in Java?

Here is the shortest version (Java 1.5+ required): repeated = new String(new char[n]). replace("\0", s); Where n is the number of times you want to repeat the string and s is the string to repeat.

How do you repeat a string in Java 8?

Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);

How do you repeat a string and time in python?

In Python, we utilize the asterisk operator to repeat a string. This operator is indicated by a “*” sign. This operator iterates the string n (number) of times. The “n” is an integer value.


2 Answers

There is a method called stringByPaddingToLength:withString:startingAtIndex::

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

Note that if you want 3 abc's, than use 9 (3 * [@"abc" length]) or create category like this:

@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 
like image 141
tig Avatar answered Oct 08 '22 04:10

tig


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" 
like image 45
Mike McMaster Avatar answered Oct 08 '22 06:10

Mike McMaster