Get the extension of a file contained in an NSString

IphoneObjective CRegexIos5Nsstring

Iphone Problem Overview


I have an NSMutable dictionary that contains file IDs and their filename+extension in the simple form of fileone.doc or filetwo.pdf. I need to determine what type of file it is to correctly display a related icon in my UITableView. Here is what I have done so far.

NSString *docInfo = [NSString stringWithFormat:@"%d", indexPath.row]; //Determine what cell we are formatting
NSString *fileType = [contentFiles objectForKey:docInfo]; //Store the file name in a string

I wrote two regex to determine what type of file I'm looking at, but they never return a positive result. I haven't used regex in iOS programming before, so I'm not entirely sure if I'm doing it right, but I basically copied the code from the Class Description page.

    NSError *error = NULL;
NSRegularExpression *regexPDF = [NSRegularExpression regularExpressionWithPattern:@"/^.*\\.pdf$/" options:NSRegularExpressionCaseInsensitive error:&error];
NSRegularExpression *regexDOC = [NSRegularExpression regularExpressionWithPattern:@"/^.*\\.(doc|docx)$/" options:NSRegularExpressionCaseInsensitive error:&error];
    NSUInteger numMatch = [regexPDF numberOfMatchesInString:fileType options:0 range:NSMakeRange(0, [fileType length])];
    NSLog(@"How many matches were found? %@", numMatch);

My questions would be, is there an easier way to do this? If not, are my regex incorrect? And finally if I have to use this, is it costly in run time? I don't know what the average amount of files a user will have will be.

Thank you.

Iphone Solutions


Solution 1 - Iphone

You're looking for [fileType pathExtension]

NSString Documentation: pathExtension

Solution 2 - Iphone

//NSURL *url = [NSURL URLWithString: fileType];
NSLog(@"extension: %@", [fileType pathExtension]);

Edit you can use pathExtension on NSString

Thanks to David Barry

Solution 3 - Iphone

Try this :

NSString *fileName = @"resume.doc";  
NSString *ext = [fileName pathExtension];

Solution 4 - Iphone

Try this, it works for me.

NSString *fileName = @"yourFileName.pdf";
NSString *ext = [fileName pathExtension];

Documentation here for NSString pathExtension

Solution 5 - Iphone

Try using [fileType pathExtension] to get the extension of the file.

Solution 6 - Iphone

In Swift 3 you could use an extension:

extension String {

public func getExtension() -> String? {
        
        let ext = (self as NSString).pathExtension
        
        if ext.isEmpty {
            return nil
        }
        
        return ext
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjer-kView Question on Stackoverflow
Solution 1 - IphoneDavid BarryView Answer on Stackoverflow
Solution 2 - IphoneremyView Answer on Stackoverflow
Solution 3 - IphoneAatish JaviyaView Answer on Stackoverflow
Solution 4 - IphoneAshuView Answer on Stackoverflow
Solution 5 - IphoneEricView Answer on Stackoverflow
Solution 6 - IphoneDomenicoView Answer on Stackoverflow