One Quickie


Truncating a string (NSString->General)
One way of truncating an NSString to a set length, and attach "..." if it got shortened.
- (NSString *) truncateString: (NSString *) string
             toCharacterCount: (NSUInteger) count {
    NSRange range = { 0, MIN(string.length, count) };
    range = [string rangeOfComposedCharacterSequencesForRange: range];
    NSString *trunc = [string substringWithRange: range];

    if (trunc.length < string.length) {
        trunc = [trunc stringByAppendingString: @"..."];
    }

    return trunc;

} // truncateString



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

webmonster@borkware.com