Search code examples
iphonensstringsubstringnsrange

Search for an array of substrings within a string


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!


Solution

  • NSString *fileName;
    NSArray *imgExtArray; // put your file extensions in here
    BOOL isImage = [imgExtArray containsObject:[fileName pathExtension]];