Request: -(BOOL)[NSString containsString:] method

Originator:cedric.luthi
Number:rdar://10482901 Date Originated:23-Nov-2011 10:38 AM
Status:Duplicate/7710615 Resolved:
Product:iPhone SDK Product Version:5.0
Classification:Enhancement Reproducible:Not Applicable
 
Summary:
I would like a containsString:(NSString *)substring method on NSString.

Notes:
Because [myString containsString:otherString] is much more readable than [myString rangeOfString:otherString].location != NSNotFound

Also, everything works as expected if myString is nil, i.e.

[nil containsString:@"x"]
returns NO :-)

[nil rangeOfString:@"x"].location != NSNotFound
returns YES :-(

Comments

Duped as 12322479

By heath.borders at Sept. 18, 2012, 7:44 p.m. (reply...)

In the meantime, use a category to add the method

@interface NSString (containsString) -(BOOL)containsString:(NSString *)string; @end

@implementation NSString (containsString)

-(BOOL)containsString:(NSString *)string { return [self rangeOfString:string].location != NSNotFound; }

@end

By philip.willoughby at Nov. 23, 2011, 1:55 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!