Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ".." from string representation of URL

Tags:

path

iphone

nsurl

When you have a filesystem path, you can have a ".." removed (and the previous path component removed as well) by using the stringByResolvingSymlinksInPath selector. How can I achieve the same thing for a URL? For example I start out with, say:

www.example.com/themes/themeA/../common/assetA.png

Which I need converted to:

www.example.com/themes/common/assetA.png
like image 749
FreeAsInBeer Avatar asked Jan 19 '23 03:01

FreeAsInBeer


1 Answers

For a URL use the NSURL method:

- (NSURL *)standardizedURL

Returns a new URL that points to the same resource as the original URL and is an absolute path.

Example:

NSString *s  = @"www.example.com/themes/themeA/../common/assetA.png";
NSURL    *u  = [NSURL URLWithString:s];
NSURL    *su = [u standardizedURL];
NSLog(@"su: %@", su);

NSLog output:

su: www.example.com/themes/common/assetA.png
like image 68
zaph Avatar answered Jan 23 '23 03:01

zaph