简体   繁体   English

如何快速检查 url 字符串是否包含单词?

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

I'm trying to forward the web view to a local file if URL doesn't contain words I want I tried many things contains and rangeofstrings didn't work for me.我试图在网络视图转发到本地文件,如果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)
        }
    }

This should do the job :这应该可以完成这项工作:

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

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

        return true
    }

    return false
}

Works for me as well:也适用于我:

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

This works for me这对我有用

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