Showing posts with label objective c. Show all posts
Showing posts with label objective c. Show all posts

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;
}

NSString enumerateLinesUsingBlock example ios

[NSString enumerateLinesUsingBlock]
Enumerates all the lines in a string.
- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block
Parameters of [NSString enumerateLinesUsingBlock]
block
The block executed for the enumeration.
The block takes two arguments:
line
The current line of the string being enumerated. The line contains just the contents of the line, without the line terminators.[NSString enumerateLinesUsingBlock]
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.
Example of [NSString enumerateLinesUsingBlock]
__block NSString *firstLine = nil;
NSString *wholeText = [[managedObject valueForKey:@"taskText"] description];
[wholeText enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    firstLine = [[line retain] autorelease];
    *stop = YES;
}];

self.textView.text = firstLine;

Example of [NSString enumerateLinesUsingBlock]
__block NSString * theLine;
[lines enumerateLinesUsingBlock:^(NSString * line, BOOL * stop){
    NSRange range = [line rangeOfString:@"Y"];
    if ( range.location != NSNotFound ) {
        theLine = [line retain];
        *stop = YES;
    }
}];

/* Use `theLine` for something */
[theLine release]; // Relinquish ownership
Example of [NSString enumerateLinesUsingBlock]
NSMutableArray *fileInput = [NSMutableArray array];

[string enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
    if ([line length] > 0) {
        [fileInput addObject:
            [line stringByTrimmingCharactersInSet:
                [NSCharacterSet whitespaceCharacterSet]];
    }
}];

NSString doubleValue example ios


doubleValue

Returns the floating-point value of the receiver’s text as a double.
- (double)doubleValue
Return Value of [NSString doubleValue]
The floating-point value of the receiver’s text as a double. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
Discussion of [NSString doubleValue]
This method skips any whitespace at the beginning of the string. This method uses formatting information stored in the non-localized value; use an NSScanner object for localized scanning of numeric values from a string.
Example of [NSString doubleValue]
static bool TextIsValidValue( NSString* newText, double &value )
{
    bool result = false;

    if ( [newText isMatchedByRegex:@"^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"] ) {
     result = true;
     value = [newText doubleValue];
    }
    return result;
}
Example of [NSString doubleValue]
NSString *test = @"22.414";
NSLog(@"doubleValue: %f", [test doubleValue]);
Example of [NSString doubleValue]
NSString *value = [valuelist objectAtIndex:valuerow];
NSString *value2 = [valuelist2 objectAtIndex:valuerow2];
double cal = [value doubleValue] + ([value2 doubleValue] * 8) + 3;
NSString *message =[[NSString alloc] initWithFormat:@"%f",cal];

NSString decomposedStringWithCompatibilityMapping example ios


decomposedStringWithCompatibilityMapping

Returns a string made by normalizing the receiver’s contents using Form KD.
- (NSString *)decomposedStringWithCompatibilityMapping
Return Value of [NSString decomposedStringWithCompatibilityMapping]
A string made by normalizing the receiver’s contents using the Unicode Normalization Form KD.
Example of [NSString decomposedStringWithCompatibilityMapping]
NSString *str = @"aąbcčdeęėfghiįjzž";
NSLog(@"%@", str);
NSMutableString *s = [[str decomposedStringWithCompatibilityMapping] mutableCopy];
NSUInteger pos = 0;
while(pos < s.length) {
    NSRange r = [s rangeOfComposedCharacterSequenceAtIndex:pos];
    if (r.location == NSNotFound) break;
    pos = ++r.location;
    if (r.length == 1) continue;
    r.length--;
    [s deleteCharactersInRange:r];
}
NSLog(@"%@", s);
Example of [NSString decomposedStringWithCompatibilityMapping]
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en-US"];

NSString *input = @"ı: ürse kün vijay 12344";

input = [input decomposedStringWithCompatibilityMapping];


NSString *output=[input stringByTrimmingCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet]];


NSString *folded = [output stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:locale];


NSLog(@"resulted String is :%@",folded);

NSString decomposedStringWithCanonicalMapping example ios


decomposedStringWithCanonicalMapping

Returns a string made by normalizing the receiver’s contents using Form D.
- (NSString *)decomposedStringWithCanonicalMapping
Return Value
A string made by normalizing the receiver’s contents using the Unicode Normalization Form D.
Example of [NSString decomposedStringWithCanonicalMapping]
NSString *eachCellContent = @"소";
NSString *searchText = @"ㅅ";

NSString *normalizedContent = [eachCellContent decomposedStringWithCanonicalMapping];
NSString *normalizedSearch = [searchText decomposedStringWithCanonicalMapping];

NSComparisonResult result = [normalizedContent compare:normalizedSearch
                                               options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch
                                                 range:NSMakeRange(0, [normalizedSearch length])
                                                locale:[NSLocale currentLocale]];
if (result == NSOrderedSame) {
    NSLog(@"same");
}
// Output: same
Example of [NSString decomposedStringWithCanonicalMapping]
NSArray *array = [NSArray arrayWithObjects:@"éli", @"bob", @"earl", @"allen", @"àli", nil];

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [(NSString*)obj1 compare:obj2 options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch];
}];

NSMutableDictionary *sectioned = [NSMutableDictionary dictionary];
NSString *firstChar = nil;

for(NSString *str in sorted)
{
    //Ignore empty strings
    if(![str length])continue;

    NSMutableArray *names = nil;

    //Compare the first character using diacritic insensitive search
    if([str compare:firstChar options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch range:NSMakeRange(0, 1)] == NSOrderedSame)
    {
        names = [sectioned objectForKey:firstChar];
    }
    else
    {
        //decomposedStringWithCanonicalMapping is where the magic happens
        //(it removes the accent mark)
        firstChar = [[str decomposedStringWithCanonicalMapping] substringToIndex:1];
        names = [NSMutableArray array];
        [sectioned setObject:names forKey:firstChar];
    }

    [names addObject:str];
}

NSLog(@"sorted: %@", sorted);
//This is sectioned like the address app
NSLog(@"sectioned: %@", sectioned);
Example of [NSString decomposedStringWithCanonicalMapping]
- (NSString*) decomposeAndFilterString: (NSString*) string
{
    NSMutableString *decomposedString = [[string decomposedStringWithCanonicalMapping] mutableCopy];
    NSCharacterSet *nonBaseSet = [NSCharacterSet nonBaseCharacterSet];
    NSRange range = NSMakeRange([decomposedString length], 0);

    while (range.location > 0) {
        range = [decomposedString rangeOfCharacterFromSet:nonBaseSet
            options:NSBackwardsSearch range:NSMakeRange(0, range.location)];
        if (range.length == 0) {
            break;
        }
        [decomposedString deleteCharactersInRange:range];
    }

    return [decomposedString autorelease];
}