我尝试在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。
答案 0 :(得分:4)
方法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