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 t开发者_运维百科ell me how to check whether the string ends with ".jpg"?
if ([[yourString pathExtension] isEqualToString:@"jpg"]){
//.jpg
}
or
if ([yourString hasSuffix:@".jpg"]){
//.jpg
}
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
}
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png' AND self BEGINSWITH[c] %@",@"img_"];
if([fltr evaluateWithObject:strPath])
{
// string matched....
}
精彩评论