Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cocoa

nsstring

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

like image 855
RAMAN RANA Avatar asked Mar 09 '11 10:03

RAMAN RANA


2 Answers

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

or

if ([yourString hasSuffix:@".jpg"]){    //.jpg } 
like image 88
Vladimir Avatar answered Sep 18 '22 13:09

Vladimir


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 } 
like image 21
ThinkBonobo Avatar answered Sep 21 '22 13:09

ThinkBonobo