Objective c 在NSURL中进行更改

Objective c 在NSURL中进行更改,objective-c,regex,nsurl,Objective C,Regex,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/im

我尝试在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:(如果您知道文件扩展名为
.gif
,则可以正常工作)

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

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
方法1:(如果您知道文件扩展名为
.gif
,则工作正常)

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

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

谢谢你,这真的为我指明了正确的方向!我使用以下代码修复了我的问题(也解决了图像扩展问题):
NSString*url=[[jsonPost objectAtIndex:countPage]objectForKey:@“摘录”]
NSArray*数组=[url组件由字符串分隔:@];NSString*retinaUrl=[NSString stringWithFormat:@“%@.%@.%@.%@.%@.%@,[array objectAtIndex:0]、[array objectAtIndex:1]、[array objectAtIndex:2]、[array objectAtIndex:3]
imageUrl=[NSURL-URLWithString:retinaUrl]谢谢这真的为我指明了正确的方向!我使用以下代码修复了我的问题(也解决了图像扩展问题):
NSString*url=[[jsonPost objectAtIndex:countPage]objectForKey:@“摘录”]
NSArray*数组=[url组件由字符串分隔:@];NSString*retinaUrl=[NSString stringWithFormat:@“%@.%@.%@.%@.%@.%@,[array objectAtIndex:0]、[array objectAtIndex:1]、[array objectAtIndex:2]、[array objectAtIndex:3]
imageUrl=[NSURL-URLWithString:retinaUrl]