Objective-C: How to extract part of a String (e.g. start with '#')

Pretty simple, easy to understand version avoiding NSRange stuff:

NSArray * words = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
NSMutableArray * mutableWords = [NSMutableArray new];
for (NSString * word in words){
    if ([word length] > 1 && [word characterAtIndex:0] == '#'){
        NSString * editedWord = [word substringFromIndex:1];
        [mutableWords addObject:editedWord];
    }
}

You can do this using an NSScanner to split the string up. This code will loop through a string and fill an array with substrings.

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:&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];

Here is how the code works:

  1. Scan up to a #. If it isn't found, the scanner will be at the end of the string.
  2. If the scanner is at the end of the string, we are done.
  3. Scan the # character so that it isn't in the output.
  4. Scan up to a space, with the characters that are scanned stored in substring. If either the # was the last character, or was immediately followed by a space, the method will return NO. Otherwise it will return YES.
  5. If characters were scanned (the method returned YES), add substring to the substrings array.
  6. GOTO 1

[aString substringWithRange:NSMakeRange(13, 10)] 

would give you substring1

You can calculate the range using:

NSRange startRange = [aString rangeOfString:@"#"];
NSRange endRange = [original rangeOfString:@"1"];

NSRange searchRange = NSMakeRange(startRange.location , endRange.location);
[aString substringWithRange:searchRange] 

would give you substring1

Read more: Position of a character in a NSString or NSMutableString

and

http://iosdevelopertips.com/cocoa/nsrange-and-nsstring-objects.html


Assuming that you are looking to find the first string that starts with a pound, and ends with a space, this might work. I don't have XCode in front of me, so forgive me if there's a syntax error or length off by 1 somewhere:

-(NSString *)StartsWithPound:(NSString *)str {
    NSRange range = [str rangeOfString:@"#"];
    if(range.length) {
        NSRange rangeend = [str rangeOfString:@" " options:NSLiteralSearch range:NSMakeRange(range.location,[str length] - range.location - 1)];
        if(rangeend.length) {
            return [str substringWithRange:NSMakeRange(range.location,rangeend.location - range.location)];
        }
        else
        {
            return [str substringFromIndex:range.location];
        }
    }
    else {
        return @"";
    }
}