Changeset 12898


Ignore:
Timestamp:
Sep 19, 2011, 1:37:43 AM (10 years ago)
Author:
livings124
Message:

roll my own method to separate a string into components

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/NSStringAdditions.m

    r12897 r12898  
    185185- (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separator
    186186{
    187     NSMutableArray * components = [NSMutableArray arrayWithArray: [self componentsSeparatedByCharactersInSet: separator]];
    188     [components removeObject: @""];
     187    NSMutableArray * components = [NSMutableArray array];
     188   
     189    for (NSUInteger i = 0; i < [self length];)
     190    {
     191        const NSRange range = [self rangeOfCharacterFromSet: separator options: 0 range: NSMakeRange(i, [self length]-i)];
     192       
     193        if (range.location != i)
     194        {
     195            NSUInteger length;
     196            if (range.location == NSNotFound)
     197                length = [self length] - i;
     198            else
     199                length = range.location - i;
     200            [components addObject: [self substringWithRange: NSMakeRange(i, length)]];
     201           
     202            if (range.location == NSNotFound)
     203                break;
     204            i += length + range.length;
     205        }
     206    }
     207    NSLog(@"%@", components);
     208   
    189209    return components;
    190210}
Note: See TracChangeset for help on using the changeset viewer.