I have a file path of, for example /Users/Documents/New York/SoHo/abc.doc
. Now I need to just retrieve /SoHo/abc.doc
from this path.
I have gone through the following:
stringByDeletingPathExtension
-> used to delete the extension from the path.stringByDeletingLastPathComponent
-> to delete the last part in the part.However I didn't find any method to delete the first part and keep the last two parts of a path.
Extension or Filename Extension or File Extension: I like the last one.
Use os. path. basename() to extract the last folder or the file from a path.
How do I get directory name from its path on a Linux or Unix-like system? [/donotprint]In other words, you can extract the directory name using dirname command.
NSString has loads of path handling methods which it would be a shame not to use...
NSString* filePath = // something
NSArray* pathComponents = [filePath pathComponents];
if ([pathComponents count] > 2) {
NSArray* lastTwoArray = [pathComponents subarrayWithRange:NSMakeRange([pathComponents count]-2,2)];
NSString* lastTwoPath = [NSString pathWithComponents:lastTwoArray];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With