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.
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
精彩评论