Remove ".." from the string representation of the URL

If you have a path to the file system, you can remove the ".." (as well as remove the previous path component) using the stringByResolvingSymlinksInPath selector. How can I achieve the same for URL? For example, I start with, say:

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

What I need to convert to:

 www.example.com/themes/common/assetA.png 
+4
source share
2 answers

For the URL, use the NSURL method:

 - (NSURL *)standardizedURL 

Returns a new URL pointing 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 
+7
source

What about the next one?

 NSString* resolved_url = [[[NSURL URLWithString: @"www.example.com/themes/themeA/../common/assetA.png"] standardizedURL] absoluteString]; 

If you want NSURL instead of NSString , remove the absoluteString call.

+2
source

Source: https://habr.com/ru/post/1385000/


All Articles