ObjC:如何将组件插入到从A/B到A/之间/B的url?

发布于 2024-12-15 02:10:25 字数 215 浏览 0 评论 0原文

我需要通过以下方式操作一些 URL 来添加组件:

/img/david/PlayBasketball.jpg

将变为:

/img/HiRes/david/PlayBasketball.jpg

,我该怎么做?

提前致谢!

I need to manipulate some URL in term of adding component to it in this way:

/img/david/PlayBasketball.jpg

will become:

/img/HiRes/david/PlayBasketball.jpg

in Objective C for iPhone, how do I do it?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2024-12-22 02:10:25

使用 NSString 方法 pathComponentspathWithComponents

NSString *p = @"/img/david/PlayBasketball.jpg";

NSMutableArray *cmps = [NSMutableArray arrayWitharray:[p pathComponents]];
// cmps will be: ["/", "img", "david", "PlayBasketball.jpg"]

[cmps insertObject:@"HIRes" atIndex:2];
// You want index 2 because "/" is at index 0, and "img" is at index 1.

NSString *newPath = [NSString pathWithComponents:cmps];
// The pathWithComponents method will add the rest of the "/"'s for you

现在,newPath 将是: @“/img/HiRes/david/PlayBasketball.jpg”

Use the NSString methods pathComponents and pathWithComponents:

NSString *p = @"/img/david/PlayBasketball.jpg";

NSMutableArray *cmps = [NSMutableArray arrayWitharray:[p pathComponents]];
// cmps will be: ["/", "img", "david", "PlayBasketball.jpg"]

[cmps insertObject:@"HIRes" atIndex:2];
// You want index 2 because "/" is at index 0, and "img" is at index 1.

NSString *newPath = [NSString pathWithComponents:cmps];
// The pathWithComponents method will add the rest of the "/"'s for you

Now, newPath will be: @"/img/HiRes/david/PlayBasketball.jpg".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文