删除 NSString 路径字符串
我有一个看起来像 /test/test1/test2
的 NSString。我如何删除 test2
,知道它可以以任何其他方式命名?谢谢!
I have a NSString which looks like /test/test1/test2
. How could I delete the test2
, knowing that it can be named in any other way? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你正在寻找 NSString 的
stringByDeletingLastPathComponent
方法(文档为您链接)。Sounds like you are looking for NSString's
stringByDeletingLastPathComponent
method (documentation linked for you).NSString 方法
- (NSString *)stringByDeletingLastPathComponent
会做你想做的事 想。但需要注意的是,它将把“/foo/bar/”变成“/foo/”,这可能是也可能不是理想的。
The NSString method
- (NSString *)stringByDeletingLastPathComponent
will do what you want.One note though, it will turn "/foo/bar/" into "/foo/" which may or may not be desirable.