0

I want to check whether a string contains any of the substrings that I place in an array. Basically, I want to search the extensions of a file, and if the file is an "image", i want certain code to execute. The only way I can think of categorizing the file as an "Image" without downloading the file is through the substring in a string method. This is my code so far:

NSString *last5Chars = [folderName substringFromIndex: [folderName length] - 5];

         NSRange textRangepdf;
         textRangepdf =[last5Chars rangeOfString:@"pdf"];

         if(textRangepdf.location != NSNotFound)
          {
         [self.itemType addObject:@"PDF.png"];
          }

Is it possible to do this where I can check if last5Chars contains @"jpg" or @"gif" of @"png" etc...?? Thanks for helping!

1
  • To basically get the extension of the file. I didn't want to just get the last 3, since some files could have a longer extension. 5 just to be cautious.
    – Prajoth
    Dec 29, 2011 at 3:59

3 Answers 3

1
NSString *fileName;
NSArray *imgExtArray; // put your file extensions in here
BOOL isImage = [imgExtArray containsObject:[fileName pathExtension]];
0
0
[folderName hasSuffix:@".jpg"] || [folderName hasSuffix:@".gif"]

obviously you can put it into a loop, if you have a whole arrayful.

0

Rather than mess about with ranges and suffixes, NSString has a method that treats an NSString as a path and returns an extension, it's called pathExtension.

Have a look in the NSString Documentation

Once you get the extension you can check it against whatever strings you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.