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

CocoaNsstring

Cocoa 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....
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRAMAN RANAView Question on Stackoverflow
Solution 1 - CocoaVladimirView Answer on Stackoverflow
Solution 2 - CocoaThinkBonoboView Answer on Stackoverflow
Solution 3 - CocoaAtif MahmoodView Answer on Stackoverflow