Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this using an <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html">NSScanner</a> to split the string up. This code will loop through a string and fill an array with substrings.</p> <pre><code>NSString * aString = @"This is the #substring1 and #subString2 I want"; NSMutableArray *substrings = [NSMutableArray new]; NSScanner *scanner = [NSScanner scannerWithString:aString]; [scanner scanUpToString:@"#" intoString:nil]; // Scan all characters before # while(![scanner isAtEnd]) { NSString *substring = nil; [scanner scanString:@"#" intoString:nil]; // Scan the # character if([scanner scanUpToString:@" " intoString:&amp;substring]) { // If the space immediately followed the #, this will be skipped [substrings addObject:substring]; } [scanner scanUpToString:@"#" intoString:nil]; // Scan all characters before next # } // do something with substrings [substrings release]; </code></pre> <p>Here is how the code works:</p> <ol> <li>Scan up to a #. If it isn't found, the scanner will be at the end of the string.</li> <li>If the scanner is at the end of the string, we are done.</li> <li>Scan the # character so that it isn't in the output.</li> <li>Scan up to a space, with the characters that are scanned stored in <code>substring</code>. If either the # was the last character, or was immediately followed by a space, the method will return NO. Otherwise it will return YES.</li> <li>If characters were scanned (the method returned YES), add <code>substring</code> to the <code>substrings</code> array.</li> <li>GOTO 1</li> </ol>
    singulars
    1. This table or related slice is empty.
    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. 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