RAMAN RANA
RAMAN RANA

Reputation: 1795

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

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

Upvotes: 61

Views: 27869

Answers (3)

Atif Mahmood
Atif Mahmood

Reputation: 8992

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

Upvotes: 2

ThinkBonobo
ThinkBonobo

Reputation: 16515

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
}

Upvotes: 6

Vladimir
Vladimir

Reputation: 170829

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

or

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

Upvotes: 142

Related Questions