将字符串一分为二并知道其元素

问题描述 投票:0回答:1

我有一个字符串,其中有一个url和一些文本! 文字可以在url之前,也可以在url之后。那么,我怎样才能把字符串一分为二,知道哪一个是文本,哪一个是URL。

例如 String 1 = " 这是堆栈溢出。https:/stackoverflow.comquestionsask" (文字在网址前)

字符串2 = "https:/stackoverflow.comquestionsask 这是堆栈溢出"(文字在url后面)

ios swift xcode swift4
1个回答
1
投票

你可以试试

let str = "This is stack overflow https://stackoverflow.com/questions/ask"
let array = str.components(separatedBy: " ")
let result = array.first { $0.contains("http") }

0
投票

如果句子中的文字与文字之间有空格,则 网址 你可以通过以下方式获取网址。

let string = "Lorem ipsum dolor sit amet"
let stringArray = string.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())

print(stringArray.last)

0
投票

我找到了解决办法。

func getAttributedText(_ text1:String,text2:String) ->NSAttributedString {

let attributedString1 = NSMutableAttributedString(string:"(text1)\n", attributes: nil)

let attributedString2 = NSMutableAttributedString(string:"(text2)\n", attributes:nil)

attributedString1.append(attributedString2)

返回 attributedString1}。

让 originalString = "https:/medium.com@felicity.johnson.mailhow-to-split-a-string-swift-3-0-e9b757445064 查新帖

                do {
                    let types: NSTextCheckingResult.CheckingType = .link
                    let detector = try? NSDataDetector(types: types.rawValue)
                    let matches = detector?.matches(in: originalString, options: .reportCompletion, range: NSMakeRange(0, originalString.count))
                    var link:String!
                    if (matches != nil){
                        for match in matches! {
                            link = (match.url?.absoluteString)!

                            break;
                        }
                    }
                    let mutableAttributedString = NSMutableAttributedString(string: originalString, attributes: nil)


                    let attributedText = getAttributedText("DOJ watchdog finds no bias in launch of Trump-Russia probe, but uncovers ‘significant’ FBI errors", text2:"The Justice Department’s inspector general, in a long-awaited review concerning the origins of the Russia investigation \n")

                    let userContent : String = ""

           // Get range of text to replace
                    if let range = mutableAttributedString.string.range(of: link){
                        let nsRange = NSRange(range, in: mutableAttributedString.string)

      // Replace content in range with the new content
                        mutableAttributedString.replaceCharacters(in: nsRange, with: userContent)
                    }
          let userTypedContent = mutableAttributedString.string.trimmingCharacters(in: .whitespaces)
                    let urlContent = attributedText.string.trimmingCharacters(in: .whitespaces)
                    UIlablel.text = urlContent + userTypedContent }

0
投票

试试这个(但要处理白色空间和Optionals)。

let sentence =  "This is stack overflow https://stackoverflow.com/questions/ask and may be some more text."

let words = sentence.components(separatedBy: " ")

if let url = words.first(where: { $0.contains("http") }) {

    let splits = sentence.components(separatedBy: url)
    let prefix = splits.first
    let suffix = splits.last

    print(prefix)   // "This is stack overflow "
    print(url)      // "https://stackoverflow.com/questions/ask"
    print(suffix)   // " and may be some more text."
}
© www.soinside.com 2019 - 2024. All rights reserved.