Showing posts with label NSString NSStringEnumerationByWords example. Show all posts
Showing posts with label NSString NSStringEnumerationByWords example. Show all posts

Tuesday, May 21, 2013

NSString NSStringEnumerationByWords example ios


String Enumeration Options - NSStringEnumerationByWords

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
};
Constants
NSStringEnumerationByLines
Enumerates by lines. Equivalent to lineRangeForRange:.
NSStringEnumerationByParagraphs
Enumerates by paragraphs. Equivalent to paragraphRangeForRange:.
NSStringEnumerationByComposedCharacterSequences
Enumerates by composed character sequences. Equivalent torangeOfComposedCharacterSequencesForRange:.
NSStringEnumerationByWords
Enumerates by words.
NSStringEnumerationBySentences
Enumerates by sentences.
[NSString NSStringEnumerationByWords]
NSStringEnumerationReverse
Causes enumeration to occur from the end of the specified range to the start.
NSStringEnumerationSubstringNotRequired
A way to indicate that the block does not need substring, in which case nil will be passed. This is simply a performance shortcut.
NSStringEnumerationLocalized
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 NSStringEnumerationByWords]
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 NSStringEnumerationByWords]
[strWord enumerateSubstringsInRange:NSMakeRange(0, [strWord length])
                            options:NSStringEnumerationByWords
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
    // ...
}];

Example of [NSString NSStringEnumerationByWords]
__block NSInteger length = 0;
[string enumerateSubstringsInRange:range
                           options:NSStringEnumerationByWords
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
    length++;
}];
Example of [NSString NSStringEnumerationByWords]
#warning I haven't tested this thoroughly
NSString *string = @"Hello";
__block NSString *firstCharacterSequence = nil;
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) 
                           options:NSStringEnumerationByWords 
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){
                            firstCharacterSequence = substring;
                            *stop = YES;
                        }];
NSLog(@"%@",firstCharacterSequence);