更改NSURL

| 我尝试在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。     
已邀请:
方法1 :(如果知道文件扩展名为extension3ѭ,则可以正常工作)
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
    

要回复问题请先登录注册