获取文件路径的最后2个目录 [英] Getting the last 2 directories of a file path

查看:69
本文介绍了获取文件路径的最后2个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件路径为例如/Users/Documents/New York/SoHo/abc.doc.现在,我只需要从该路径中检索/SoHo/abc.doc.

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.

我经历了以下事情:

  • stringByDeletingPathExtension->用于从路径中删除扩展名.
  • stringByDeletingLastPathComponent->删除零件的最后一部分.
  • 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.

推荐答案

NSString包含大量的路径处理方法,如果不使用它会很可惜...

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];
}

这篇关于获取文件路径的最后2个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆