gpt4 book ai didi

ios - 如何通过 Swift3 将 HTML 字符串分离成数组或字典?

转载 作者:行者123 更新时间:2023-11-28 21:00:42 24 4
gpt4 key购买 nike

我像这样从 API 得到 HTML 字符串:

let a: String = "<a href="https://www.google.com.tw">https://www.google.com.tw </a>"
let b: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a>Hello Tim"
let c: String = "<a href="myAppName://app/user/aa3b77411825b88b318d77gg">@Tim </a><a href="https://www.google.com.tw">https://www.google.com.tw </a>"

let splitedArray1: [String] = a.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray2: [String] = b.componentsSeparatedByString("?????") //splited string which is the best
let splitedArray3: [String] = c.componentsSeparatedByString("?????") //splited string which is the best

我想将链接与它们分开并获取如下数据

print(splitedArray1) //["https://www.google.com.tw","https://www.google.com.tw"]
print(splitedArray2) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
print(splitedArray3) //["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]

最佳答案

可能的解决方案:使用NSAttributedString 然后在NSLinkAttributeName 上枚举,如果没有,则表示没有链接标记,因此您只需保留“字符串” ,否则,您添加链接,然后添加字符串。

在 Playground 中快速编写:

let a: String = "<a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"
let b: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a>Hello Tim"
let c: String = "<a href=\"myAppName://app/user/aa3b77411825b88b318d77gg\">@Tim </a><a href=\"https://www.google.com.tw\">https://www.google.com.tw </a>"

let values:[String] = [a, b, c]



for aHTMLString in values
{
let attributedString = try! NSAttributedString.init(data: aHTMLString.data(using: .utf8)!,
options: [.documentType: NSAttributedString.DocumentType.html],
documentAttributes: nil)
var retValues = [String]()
attributedString.enumerateAttribute(.link,
in: NSRange(location: 0, length: attributedString.string.count),
options: [],
using: { (attribute, range, pointerStop) in
if let attribute = attribute as? URL
{
retValues.append(attribute.absoluteString)
}
let subString = (attributedString.string as NSString).substring(with: range)
retValues.append(subString)
})

print("*** retValues: \(retValues)")
}

let targetResult1 = ["https://www.google.com.tw","https://www.google.com.tw"]
let targetResult2 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","Hello Tim"]
let targetResult3 = ["myAppName://app/user/aa3b77411825b88b318d77gg","@Tim ","https://www.google.com.tw","https://www.google.com.tw "]
print("targetResult1: \(targetResult1)")
print("targetResult2: \(targetResult2)")
print("targetResult3: \(targetResult3)")

输出:

*** retValues: ["https://www.google.com.tw/", "https://www.google.com.tw "]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
*** retValues: ["myappname://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw/", "https://www.google.com.tw "]
targetResult1: ["https://www.google.com.tw", "https://www.google.com.tw"]
targetResult2: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "Hello Tim"]
targetResult3: ["myAppName://app/user/aa3b77411825b88b318d77gg", "@Tim ", "https://www.google.com.tw", "https://www.google.com.tw "]

有细微差别,我复制了您的“目标”(splitArray),最后一个缺少一个空格,我的代码倾向于在链接上添加最后一个“/”。

关于ios - 如何通过 Swift3 将 HTML 字符串分离成数组或字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com