I have an NSString with the value of
http://digg.com/news/business/24hr
How can I get everything before the 3rd level?
http://digg.com/news/
I have an NSString with the value of
http://digg.com/news/business/24hr
How can I get everything before the 3rd level?
http://digg.com/news/
This isn't exactly the third level, mind you. An URL is split like that way:
http
)://
delimiterusername:password@hostname
)digg.com
):80
after the domain name for instance)/news/business/24hr
)?foo=bar&baz=frob
)#foobar
).A "fully-featured" URL would look like this:
http://foobar:[email protected]:8080/some/path/file.html?foo=bar#baz
As you can see, it can get quite lengthy and can bear lots of informations. Depending on the origin of your URL, you might want to do more or less elaborate concatenations. Though, for your example URL, what you seem to want is the protocol, the host and the first path component.
NSURL
has a wide range of accessors. You may check them in the documentation for the NSURL
class, section Accessing the Parts of the URL. You can see their effect on this page (scroll down a little). What you'll want, though, is something like that:
NSURL* url = [NSURL urlWithString:@"http://digg.com/news/business/24hr"];
NSString* reducedUrl = [NSString stringWithFormat:
@"%@://%@/%@",
url.scheme,
url.host,
[url.pathComponents objectAtIndex:0]];