One Quickie


Stripping out newlines from a string (NSString->General)
So you have an NSString and want to yank out the newlines. You can do a splitand join, like in scripting languages, or you can make a mutable copy and manipulate that:
    NSMutableString *mstring = [NSMutableString stringWithString:string];
    NSRange wholeShebang = NSMakeRange(0, [mstring length]);

    [mstring replaceOccurrencesOfString: @"
"
             withString: @""
             options: 0
             range: wholeShebang];

    return [NSString stringWithString: mstring];
(this can also be used for generic string manipulations, not just stripping out newlines).

This technique takes half the time (at least) of split/join. But probably not enough to make an impact. In a simple test, split/join took 0.124 seconds to strip 36909 newlines in a 1.5 meg textfile, and 0.071 seconds to do the same.



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com