Tuesday, May 21, 2013

NSString NSStringEnumerationSubstringNotRequired example ios

String Enumeration Options - NSStringEnumerationSubstringNotRequired

Constants to specify kinds of substrings and styles of enumeration.
typedef NSUInteger NSStringEnumerationOptions;
enum {
NSStringEnumerationByLines = 0,
NSStringEnumerationByParagraphs = 1,
NSStringEnumerationByComposedCharacterSequences = 2,
NSStringEnumerationByWords = 3,
NSStringEnumerationBySentences = 4,
NSStringEnumerationReverse = 1UL << 8,
NSStringEnumerationSubstringNotRequired = 1UL << 9,
NSStringEnumerationLocalized = 1UL << 10
    Enumerates by lines. Equivalent to lineRangeForRange:.
    Enumerates by paragraphs. Equivalent to paragraphRangeForRange:.
    Enumerates by composed character sequences. Equivalent torangeOfComposedCharacterSequencesForRange:.
    Enumerates by words.
    Enumerates by sentences.
    [NSString NSStringEnumerationSubstringNotRequired]
    Causes enumeration to occur from the end of the specified range to the start.
    A way to indicate that the block does not need substring, in which case nil will be passed. This is simply a performance shortcut.
    Causes the enumeration to occur using user's default locale. This does not make a difference in line, paragraph, or composed character sequence enumeration, but it may for words or sentences.

Discussion of [NSString NSStringEnumerationSubstringNotRequired]
These options are used with theenumerateSubstringsInRange:options:usingBlock: method. Pass in oneNSStringEnumerationBy... option and combine with any of the remaining enumeration style constants using the C bitwise OR operator.
Example of [NSString NSStringEnumerationSubstringNotRequired]
- (NSUInteger)numberOfWordsInString:(NSString *)str {
    __block NSUInteger count = 0;
    [str enumerateSubstringsInRange:NSMakeRange(0, [str length])
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    return count;

Example of [NSString NSStringEnumerationSubstringNotRequired]
NSString *removeLastWord(NSString *str) {
    __block NSRange lastWordRange = NSMakeRange([str length], 0);
    NSStringEnumerationOptions opts = NSStringEnumerationByWords | NSStringEnumerationReverse | NSStringEnumerationSubstringNotRequired;
    [str enumerateSubstringsInRange:NSMakeRange(0, [str length]) options:opts usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        lastWordRange = substringRange;
        *stop = YES;
    return [str substringToIndex:lastWordRange.location];
Example of [NSString NSStringEnumerationSubstringNotRequired]
NSString *substringFromFourthWord(NSString *input) {
    __block NSUInteger index = NSNotFound;
    __block NSUInteger count = 0;
    [input enumerateSubstringsInRange:NSMakeRange(0, [input length]) options:(NSStringEnumerationByWords|NSStringEnumerationSubstringNotRequired) usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        if (++count == 4) {
            // found the 4th word
            index = substringRange.location;
            *stop = YES;
    if (index == NSNotFound) {
        return @"";
    } else {
        return [input substringFromIndex:index];