114

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/
zneak
  • 134,922
  • 42
  • 253
  • 328
Melina
  • 1,463
  • 4
  • 13
  • 13

2 Answers2

437

This isn't exactly the third level, mind you. An URL is split like that way:

  • the protocol or scheme (here, http)
  • the :// delimiter
  • the username and the password (here there isn't any, but it could be username:password@hostname)
  • the host name (here, digg.com)
  • the port (that would be :80 after the domain name for instance)
  • the path (here, /news/business/24hr)
  • the parameter string (anything that follows a semicolon)
  • the query string (that would be if you had GET parameters like ?foo=bar&baz=frob)
  • the fragment (that would be if you had an anchor in the link, like #foobar).

A "fully-featured" URL would look like this:

http://foobar:nicate@example.com:8080/some/path/file.html;params-here?foo=bar#baz

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. For quick reference:

  • -[NSURL scheme] = http
  • -[NSURL resourceSpecifier] = (everything from // to the end of the URL)
  • -[NSURL user] = foobar
  • -[NSURL password] = nicate
  • -[NSURL host] = example.com
  • -[NSURL port] = 8080
  • -[NSURL path] = /some/path/file.html
  • -[NSURL pathComponents] = @["/", "some", "path", "file.html"] (note that the initial / is part of it)
  • -[NSURL lastPathComponent] = file.html
  • -[NSURL pathExtension] = html
  • -[NSURL parameterString] = params-here
  • -[NSURL query] = foo=bar
  • -[NSURL fragment] = baz

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[1]];

For your example URL, what you seem to want is the protocol, the host and the first path component. (The element at index 0 in the array returned by -[NSString pathComponents] is simply "/", so you'll want the element at index 1. The other slashes are discarded.)

zneak
  • 134,922
  • 42
  • 253
  • 328
  • 9
    for anyone reading this: you need to get `[url.pathComponents objectAtIndex:1]` instead of index `0`, because the slashes are actually elements of the array, causing the code in this answer to output `http://digg.com//` – Dima Aug 31 '12 at 21:41
  • @Dima, good catch. However, only the first slash is considered a path component, the others are discarded. I'm not sure why, though. – zneak Sep 01 '12 at 05:36
  • Are we missing the filename from the bullet point list? In this example, `file.html` isn't listed in the bullet points--I see we're jumping from the path to the query string... – Brett Oct 04 '12 at 02:00
  • @Brett, the file name is not a "section" of the URL itself, that's why I didn't list it–it's just part of the "file path"–but if you're interested, you can access it with `lastPathComponent`. – zneak Oct 04 '12 at 06:09
  • What would be the best approach to get a subdomain out of the above example e.g. the 'mobile' subdomain - http://mobile.digg.com/ – Ríomhaire Aug 12 '14 at 18:06
  • @Ríomhaire, string manipulations are your best bet. (split, substring, the choice is yours) – zneak Aug 12 '14 at 21:32
  • 2
    digg.com? how old is this post! ;-) – Deprecated Darren Feb 11 '15 at 16:52
7

Using Playground , language swift, I got this ! Playground provides an interactive way of seeing this in action. I hope you can enjoy doing the same, a fun way to learn NSURL an important topic in iOS.

Kumar Utsav
  • 2,761
  • 4
  • 24
  • 38