簡體   English   中英

如何快速檢查 url 字符串是否包含單詞?

[英]How do i check if url string contains a word in swift?

我試圖在網絡視圖轉發到本地文件,如果URL中不包含的話我想我嘗試了很多東西包含rangeofstrings沒有為我工作。

  if let url = "http://facebook.com/url/url"{

        if url.contains("facebook.com") || url.contains("nocontent") || url.contains("nointernet") || url.contains("paypal.com"){
            //Doing something here
            return true
        }else{

            let htmlFile = Bundle.main.path(forResource: "nocontent", ofType: "html")
            let html = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
            webView.loadHTMLString(html!, baseURL: nil)
        }
    }

這應該可以完成這項工作:

if let url = URL(string: "http://facebook.com/url/url") {

    if url.absoluteString.range(of: "facebook.com") != nil {

        return true
    }

    return false
}

也適用於我:

if url.absoluteString.contains("facebook.com") {
    // do something here
}

這對我有用

guard url.path.contains("facebook.com")  else { return nil }
 //do what you want

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM