获取 URL 字符串的域部分?

发布于 2024-11-10 09:35:27 字数 189 浏览 0 评论 0原文

我有一个带有 URL 的 NSString,这样:

http://someurl.com/something

我如何仅获取 someurl.com ? 我已经尝试过 substringToIndex 但没有成功:(

提前致谢:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

盗心人 2024-11-17 09:35:27

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

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
一场信仰旅途 2024-11-17 09:35:27

使用

[[NSURL URLWithString:@"http://someurl.com/something"] host]

Use

[[NSURL URLWithString:@"http://someurl.com/something"] host]
╄→承喏 2024-11-17 09:35:27

Swift 4.2

我编写了 URL 扩展来获取 SLD。看来,有n

extension URL {
    /// second-level domain [SLD]
    ///
    /// i.e. `msk.ru, spb.ru`
    var SLD: String? {
        return host?.components(separatedBy: ".").suffix(2).joined(separator: ".")
    }
}

Swift 4.2

I wrote extension for URL to take SLD. Seems, there is n

extension URL {
    /// second-level domain [SLD]
    ///
    /// i.e. `msk.ru, spb.ru`
    var SLD: String? {
        return host?.components(separatedBy: ".").suffix(2).joined(separator: ".")
    }
}
别闹i 2024-11-17 09:35:27

Swift 5

字符串扩展,为您提供域名,同时删除“www”。

extension String {
func StripURLMakeAttractive(_ url: String) -> String {
        
    let url = URL(string: url)
    guard let domain = url?.host else {return "error"}
    var modifiedDomain: String = ""
    
    var checkWWW: String = ""
    let checkIndexes = [0,1,2,3]

    for (index,char) in domain.enumerated() {
        if checkIndexes.contains(index){
            checkWWW.append(char)
        }
        if index > 3 && checkWWW.lowercased() == "www."  {
            modifiedDomain.append(char)
        }
    }
    
    if checkWWW.lowercased() == "www."{
        return modifiedDomain
    }
    else {
        return domain
    }
    
}

}

Swift 5

Extension to string, that gives you the domain name, while also removing "www."

extension String {
func StripURLMakeAttractive(_ url: String) -> String {
        
    let url = URL(string: url)
    guard let domain = url?.host else {return "error"}
    var modifiedDomain: String = ""
    
    var checkWWW: String = ""
    let checkIndexes = [0,1,2,3]

    for (index,char) in domain.enumerated() {
        if checkIndexes.contains(index){
            checkWWW.append(char)
        }
        if index > 3 && checkWWW.lowercased() == "www."  {
            modifiedDomain.append(char)
        }
    }
    
    if checkWWW.lowercased() == "www."{
        return modifiedDomain
    }
    else {
        return domain
    }
    
}

}

农村范ル 2024-11-17 09:35:27

雨燕5.5

extension String {
    var onlyHost: String? {
        let url = URL(string: self)
        return url?.host
    }
}

Swift 5.5

extension String {
    var onlyHost: String? {
        let url = URL(string: self)
        return url?.host
    }
}
第七度阳光i 2024-11-17 09:35:27

您应该查看 主机NSURL 类。

You should look at the host() method of the NSURL class.

烧了回忆取暖 2024-11-17 09:35:27

Swift 4

如果您使用自定义 url 方案,则表示深度链接,例如:(

myapp:homescreen

并且没有“带有主机名的正斜杠”(//www. bbc)),对我来说提取“主屏幕”的一种解决方案是使用下面的索引方法,然后使用模式匹配来抓取 ":" 之后的所有内容。

let index = absoluteString.index(absoluteString.startIndex, offsetBy: 5)
String(absoluteString[index...])

url.hostabsoluteURL.host 在这种情况下为 nil

Swift 4

And if you are using Custom url schemes say for deeplinking, for eg:

myapp:homescreen

(and don't have the "forward slashes with host name" (//www.bbc)), one solution that worked for me to extract "homescreen" is by using the index method below and then pattern matching to scrape everything after ":"

let index = absoluteString.index(absoluteString.startIndex, offsetBy: 5)
String(absoluteString[index...])

url.host or absoluteURL.host is nil in this scenario

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文