How can I check that the NSString ends with the certain character(.jpg)?
CocoaNsstringCocoa Problem Overview
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"?
Cocoa Solutions
Solution 1 - Cocoa
if ([[yourString pathExtension] isEqualToString:@"jpg"]){
//.jpg
}
or
if ([yourString hasSuffix:@".jpg"]){
//.jpg
}
Solution 2 - Cocoa
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
}
Solution 3 - Cocoa
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png' AND self BEGINSWITH[c] %@",@"img_"];
if([fltr evaluateWithObject:strPath])
{
// string matched....
}