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 :)
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();
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.
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.
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
Use
[[NSURL URLWithString:@"http://someurl.com/something"] host]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With