Monday, May 13, 2013

NSString enumerateSubstringsInRange example ios


enumerateSubstringsInRange: options: usingBlock:

Enumerates the substrings of the specified type in the specified range of the string.
- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)optsusingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block
Parameters
range
The range within the string to enumerate substrings.
opts
Options specifying types of substrings and enumeration styles.
block
The block executed for the enumeration.
The block takes four arguments:
substring
The enumerated string.
substringRange[NSString enumerateSubstringsInRange]
The range of the enumerated string in the receiver.
enclosingRange
The range that includes the substring as well as any separator or filler characters that follow. For instance, for lines, enclosingRange contains the line terminators. The enclosingRange for the first string enumerated also contains any characters that occur before the string. Consecutive enclosing ranges are guaranteed not to overlap, and every single character in the enumerated range is included in one and only one enclosing range.
stop
A reference to a Boolean value that the block can use to stop the enumeration by setting *stop = YES; it should not touch *stop otherwise.
Discussion of [NSString enumerateSubstringsInRange]
If this method is sent to an instance of NSMutableString, mutation (deletion, addition, or change) is allowed, as long as it is within enclosingRange. After a mutation, the enumeration continues with the range immediately following the processed range, after the length of the processed range is adjusted for the mutation. (The enumerator assumes any change in length occurs in the specified range.)[NSString enumerateSubstringsInRange]
For example, if the block is called with a range starting at location N, and the block deletes all the characters in the supplied range, the next call will also pass N as the index of the range. This is the case even if mutation of the previous range changes the string in such a way that the following substring would have extended to include the already enumerated range. For example, if the string "Hello World" is enumerated via words, and the block changes "Hello " to "Hello", thus forming "HelloWorld", the next enumeration will return "World" rather than "HelloWorld".
Example of [NSString enumerateSubstringsInRange]
[s enumerateSubstringsInRange:NSMakeRange(0, [s length])
                      options:NSStringEnumerationByWords
                   usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                       NSLog(@"%@", substring);
                   }];
Example of [NSString enumerateSubstringsInRange]
[aString enumerateSubstringsInRange:NSMakeRange(0, [aString length])
                            options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
    if ([substring rangeOfString:@"ll" options:NSCaseInsensitiveSearch].location != NSNotFound)
        /* do whatever */;
}];
Example of [NSString enumerateSubstringsInRange]
NSArray *fullSentencesFromText(NSString *text) {
    NSMutableArray *results = [NSMutableArray array];
    [text enumerateSubstringsInRange:NSMakeRange(0, [text length]) options:NSStringEnumerationBySentences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [results addObject:substring];
    }];
    return results;
}