获取文件路径的最后2个目录

jam*_*obo 9 iphone xcode objective-c ipod-touch ios

例如,我有一个文件路径/Users/Documents/New York/SoHo/abc.doc.现在我需要/SoHo/abc.doc从这条路径中检索.

我经历了以下几点:

  • stringByDeletingPathExtension - >用于从路径中删除扩展名.
  • stringByDeletingLastPathComponent - >删除部件中的最后一部分.

但是我没有找到任何方法来删除第一部分并保留路径的最后两部分.

jba*_*100 11

NSString有很多路径处理方法,如果不使用它将是一种耻辱...

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];
}
Run Code Online (Sandbox Code Playgroud)


Tig*_*ing 0

为什么不搜索“/”字符并以这种方式确定路径?