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

Tuesday, May 21, 2013

NSString NSStringEnumerationLocalized example ios

String Enumeration Options - NSStringEnumerationLocalized
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 NSStringEnumerationLocalized]
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 NSStringEnumerationLocalized]
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 NSStringEnumerationLocalized]
NSString *msg = @"Hi, I am testing. How are you? Wow!! this is the best, and I am happy.";
[msg enumerateSubstringsInRange:NSMakeRange(0, [msg length])
                        options:NSStringEnumerationBySentences | NSStringEnumerationLocalized
                     usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
    NSLog(@"Sentence:%@", substring);       
    // Add each sentence into an array                                                                 
}];
Example of [NSString NSStringEnumerationLocalized]
[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 NSStringEnumerationLocalized]
NSString     *string     = @"This is a test. This is only a test.";
NSCountedSet *countedSet = [NSCountedSet new];

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                           options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                            // This block is called once for each word in the string.
                            [countedSet addObject:substring];

                            // If you want to ignore case, so that "this" and "This" 
                            // are counted the same, use this line instead to convert
                            // each word to lowercase first:
                            // [countedSet addObject:[substring lowercaseString]];
                        }];

NSLog(@"%@", countedSet);

// Results:  2012-11-13 14:01:10.567 Testing App[35767:fb03] 
// <NSCountedSet: 0x885df70> (a [2], only [1], test [2], This [2], is [2])

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
};
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 NSStringEnumerationSubstringNotRequired]
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 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])
                            options:NSStringEnumerationByWords|NSStringEnumerationSubstringNotRequired
                         usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        count++;
    }];
    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];
    }
}

NSString NSStringEnumerationReverse example ios


String Enumeration Options - NSStringEnumerationReverse

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 NSStringEnumerationReverse]
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 NSStringEnumerationReverse]
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 NSStringEnumerationReverse]
__block NSString *lastWord = nil;

[someNSStringHere enumerateSubstringsInRange:NSMakeRange(0, [someNSStringHere length]) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
    lastWord = substring;
    *stop = YES;
}];

Example of [NSString NSStringEnumerationReverse]
NSString *myString = [NSString stringWithString:@"abcdefghijklmnopqrstuvwxyz"];
NSMutableString *reversedString = [NSMutableString stringWithCapacity:[myString length]];

[myString enumerateSubstringsInRange:NSMakeRange(0,[myString length]) 
                           options:(NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences)
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                            [reversedString appendString:substring];
                        }];

// reversedString is now zyxwvutsrqponmlkjihgfedcba
Example of [NSString NSStringEnumerationReverse]
- (NSString *)reverseString:(NSString *)string {
    NSMutableString *reversedString = [[NSMutableString alloc] init];
    NSRange fullRange = [string rangeOfString:string];
    NSStringEnumerationOptions enumerationOptions = (NSStringEnumerationReverse | NSStringEnumerationByComposedCharacterSequences);
    [string enumerateSubstringsInRange:fullRange options:enumerationOptions usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        [reversedString appendString:substring];
    }];
    return reversedString;
}