Reputation: 1795
I have a NSString object which is assigned this ("http://vspimages.vsp.virginia.gov/images/024937-02.jpg"). Can anybody tell me how to check whether the string ends with ".jpg"?
Upvotes: 61
Views: 27869
Reputation: 8992
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png' AND self BEGINSWITH[c] %@",@"img_"];
if([fltr evaluateWithObject:strPath])
{
// string matched....
}
Upvotes: 2
Reputation: 16515
appending to vladimir's answer, you may wish to make a case insensitive comparison. Here's how I did it:
if( [[yourString pathExtension] caseInsensitiveCompare:@"jpg"] == NSOrderedSame ) {
// strings are equal but may not be same case
}
Upvotes: 6
Reputation: 170829
if ([[yourString pathExtension] isEqualToString:@"jpg"]){
//.jpg
}
or
if ([yourString hasSuffix:@".jpg"]){
//.jpg
}
Upvotes: 142