When I have NSString with '/Users/user/Projects/thefile.ext' I want to extract 'thefile' with Objective-C methods.
What is the easiest way to do that?
Thanks.
When I have NSString with '/Users/user/Projects/thefile.ext' I want to extract 'thefile' with Objective-C methods.
What is the easiest way to do that?
Thanks.
[[[string lastPathComponent] componentsSeparatedByString: @"."] objectAtIndex: 0];
From The NSString reference, you can use :
NSString* theFileName = [[string lastPathComponent] stringByDeletingPathExtension]
The lastPathComponent call will return "thefile.ext", and the stringByDeletingPathExtension will remove the .ext from the end.
If you're displaying a user-readable file name, you do not want to use lastPathComponent
. Instead, pass the full path to NSFileManager's displayNameAtPath:
method. This basically does does the same thing, only it correctly localizes the file name and removes the extension based on the user's preferences.