Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the domain part of an URL string?

I have an NSString with an URL, in this way:

http://someurl.com/something 

How would I get someurl.com only? I've already tried substringToIndex with no luck :(

Thanks in advance :)

like image 268
pmerino Avatar asked May 28 '11 15:05

pmerino


People also ask

How do I find the domain of a string?

Firstly, we'd need to extract the host from the given URL value. We can use the URI class: String urlString = "https://www.baeldung.com/java-tutorial"; URI uri = new URI(urlString); String host = uri. getHost();

How do I extract a domain from a URL in Google Sheets?

The =REGEXREPLACE() function is built-in Google Sheets and it extracts domains from URLs. What's great about is it's only a simple line of code that you can paste into your cell. The function is not super technical and you can change it any way you see fit.

How do I find the domain of a URL in Python?

To get the domain from a URL in Python, the easiest way is to use the urllib. parse module urlparse() function and access the netloc attribute.


2 Answers

Objective-C

NSString* urlString = @"http://someurl.com/something"; NSURL* url = [NSURL URLWithString:urlString]; NSString* domain = [url host]; 

Swift 2

var urlString = "http://someurl.com/something" var url = NSURL(string: urlString) var domain = url?.host 

Swift 3+

var urlString = "http://someurl.com/something" var url = URL(string: urlString) var domain = url?.host 
like image 198
Stephen Darlington Avatar answered Oct 22 '22 12:10

Stephen Darlington


Use

[[NSURL URLWithString:@"http://someurl.com/something"] host] 
like image 41
Deepak Danduprolu Avatar answered Oct 22 '22 11:10

Deepak Danduprolu