Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using Cocoa in Python, then you can just do that, as PyObjC imbues <code>NSString</code> with all of the Python <code>unicode</code> class's abilities.</p> <p>Otherwise, there are two ways.</p> <p>One is to create an array with the same string in it <code>n</code> times, and use <code>componentsJoinedByString:</code>. Something like this:</p> <pre><code>NSMutableArray *repetitions = [NSMutableArray arrayWithCapacity:n]; for (NSUInteger i = 0UL; i &lt; n; ++i) [repetitions addObject:inputString]; outputString = [repetitions componentsJoinedByString:@""]; </code></pre> <p>The other way would be to start with an empty <code>NSMutableString</code> and append the string to it <code>n</code> times, like this:</p> <pre><code>NSMutableString *temp = [NSMutableString stringWithCapacity:[inputString length] * n]; for (NSUInteger i = 0UL; i &lt; n; ++i) [temp appendString:inputString]; outputString = [NSString stringWithString:temp]; </code></pre> <p>You may be able to cut out the <code>stringWithString:</code> call if it's OK for you to return a mutable string here. Otherwise, you probably should return an immutable string, and the <code>stringWithString:</code> message here means you have two copies of the string in memory.</p> <p>Therefore, I recommend the <code>componentsJoinedByString:</code> solution.</p> <p>[Edit: Borrowed idea to use <code>…WithCapacity:</code> methods from <a href="https://stackoverflow.com/questions/260945/create-nsstring-by-repeating-another-string-a-given-number-of-times#260969">Mike McMaster's answer</a>.]</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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