Extract part of URL

2019-01-20 05:33发布

问题:

I have an URL and need to extract a part of it.

The url is like this:
http://website.com/get.php?url=http://www.website2.com/index.html?articleID=123456

And I need to extract this part:
http://www.website2.com/index.html?articleID=123456

How would I do this in Objective-C?

回答1:

First of all, create a NSURL. Then, use the querymethod to get the query string part:

NSURL    * url = [ NSURL URLWithString: @"... your url ..." ];
NSString * q   = [ url query ];

Then you can use the NSString methods to isolate the needed part.



回答2:

See the NSString -rangeOfSubstring: and -componentsSeparatedByString: methods.

In this case, you could just split the string at the "=".