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. Commented 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]];
Sign up to request clarification or add additional context in comments.

Comments

0
[folderName hasSuffix:@".jpg"] || [folderName hasSuffix:@".gif"]

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

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.