Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate NSString by repeating another string a given number of times
    text
    copied!<p>This should be easy, but I'm having a hard time finding the easiest solution.</p> <p>I need an <code>NSString</code> that is equal to another string concatenated with itself a given number of times.</p> <p>For a better explanation, consider the following python example:</p> <pre><code>&gt;&gt; original = "abc" "abc" &gt;&gt; times = 2 2 &gt;&gt; result = original * times "abcabc" </code></pre> <p>Any hints?</p> <hr> <p>EDIT:</p> <p>I was going to post a solution similar to the one by <a href="https://stackoverflow.com/questions/260945/create-nsstring-by-repeating-another-string-a-given-number-of-times#260969">Mike McMaster's answer</a>, after looking at this implementation from the OmniFrameworks:</p> <pre><code>// 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 &lt; aLength) { [spacesLock lock]; while (spacesLength &lt; aLength) { [spaces appendString:spaces]; spacesLength += spacesLength; } [spacesLock unlock]; } return [spaces substringToIndex:aLength]; } </code></pre> <p>Code reproduced from the file:</p> <pre><code>Frameworks/OmniFoundation/OpenStepExtensions.subproj/NSString-OFExtensions.m </code></pre> <p>on the OpenExtensions framework from the <a href="http://www.omnigroup.com/developer/" rel="noreferrer">Omni Frameworks</a> by <a href="http://www.omnigroup.com/" rel="noreferrer">The Omni Group</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload