60

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"?

Matt Becker
  • 2,338
  • 1
  • 28
  • 36
RAMAN RANA
  • 1,785
  • 4
  • 21
  • 40

3 Answers3

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

or

if ([yourString hasSuffix:@".jpg"]){
   //.jpg
}
Vladimir
  • 170,431
  • 36
  • 387
  • 313
6

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
}
StenaviN
  • 3,687
  • 24
  • 34
ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
2
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png' AND self BEGINSWITH[c] %@",@"img_"];
if([fltr evaluateWithObject:strPath])
{
    // string matched....
}
Atif Mahmood
  • 8,882
  • 2
  • 41
  • 44