How can I check that the NSString ends with the certain character(.jpg)?

Solution 1:

if ([[yourString pathExtension] isEqualToString:@"jpg"]){
   //.jpg
}

or

if ([yourString hasSuffix:@".jpg"]){
   //.jpg
}

Solution 2:

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:

NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png' AND self BEGINSWITH[c] %@",@"img_"];
if([fltr evaluateWithObject:strPath])
{
    // string matched....
}