views:

111

answers:

3

Coming from a ruby background, I'm wishing NSString had some convenient methods like the following:

[@"John" contains:@"ohn"] => BOOL
[@"1,2,3,4," split:@","] => NSArray

Anyone know of such an NSString helper library?

+6  A: 

Try this:

BOOL contains = ([@"John" rangeOfString:@"ohn"].length == 0 ? NO : YES);
NSArray *array = [@"1,2,3,4" componentsSeparatedByString:@","];
pheelicks
+2  A: 

http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Strings/Articles/SearchingStrings.html#//apple_ref/doc/uid/20000149-CJBBGBAI

NSString *searchString = @"age";

NSString *beginsTest = @"Agencies";
NSRange prefixRange = [beginsTest rangeOfString:searchString
    options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];

// prefixRange = {0, 3}
Robert
+4  A: 

Some "quickies" for the NSString class, quite a handy reference to keep around: http://borkware.com/quickies/one?topic=NSString

alku83