更改 NSURL

发布于 2024-10-31 22:01:16 字数 427 浏览 0 评论 0原文

我尝试在 NSURL 中插入一些内容。我从 json 字符串获取 URL:

imageUrl = [NSURL URLWithString:[[jsonPost objectAtIndex:countPage] objectForKey:@"excerpt"]];

imageURL 现在包含以下内容:

http://www.site.com/images/uploads/2011/04/2010-12-14-image.gif

我试图实现的是将 URL 更改为:

http://www.site.com/images/uploads/2011/04/2010-12-14-image_r.gif

因此,在扩展名之前将添加 _r 。

I try to insert something within an NSURL. I get my URL from a jSon string:

imageUrl = [NSURL URLWithString:[[jsonPost objectAtIndex:countPage] objectForKey:@"excerpt"]];

The imageURL now contains the following:

http://www.site.com/images/uploads/2011/04/2010-12-14-image.gif

What i try to achieve is to change the URL to:

http://www.site.com/images/uploads/2011/04/2010-12-14-image_r.gif

So there will be _r added right before the extension.

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

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

发布评论

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

评论(1

救星 2024-11-07 22:01:16

方法 1:(如果您知道文件扩展名是 .gif,则可以正常工作)

NSString *string = [[jsonPost objectAtIndex:countPage] objectForKey:@"excerpt"]];
NSArray *array = [string componentsSeparatedByString:@".gif"]; //separated by dot
NSString *editedString = [NSString stringWithFormat:@"%@_r.gif",[array objectAtIndex:0]];
NSURL *url = [NSURL urlWithString:editedString];

编辑:
更可靠的解决方案:

NSRange range;
range.location = 0;//starting from the first character
range.length = string.length - 4;//excluding the last 4 characters
//of course you have to make sure the .extension part is 4 characters long(at least fixed length), not like .torrent or .rmvb
NSString *newString = [urlString substringWithRange:range];//this should give you the url part without the file extension

//then append the newString with something and make your url with this string

method 1:(works ok if you know the file extension to be .gif)

NSString *string = [[jsonPost objectAtIndex:countPage] objectForKey:@"excerpt"]];
NSArray *array = [string componentsSeparatedByString:@".gif"]; //separated by dot
NSString *editedString = [NSString stringWithFormat:@"%@_r.gif",[array objectAtIndex:0]];
NSURL *url = [NSURL urlWithString:editedString];

edit:
a more reliable solution:

NSRange range;
range.location = 0;//starting from the first character
range.length = string.length - 4;//excluding the last 4 characters
//of course you have to make sure the .extension part is 4 characters long(at least fixed length), not like .torrent or .rmvb
NSString *newString = [urlString substringWithRange:range];//this should give you the url part without the file extension

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