swift - 将具有特殊字符的字符串分隔成数组

标签 swift string unicode

所以我正在通过开发 Blogger 阅读器应用程序来学习如何使用 JSON。我想在表格中显示它在博客条目中的图像,所以当我下载代码时,我有这样的东西:

"content = "<div style=\"font-family: 'Open Sans', Arial, sans-serif; font-size: 14px; margin-bottom: 15px; padding: 0px; text-align: justify;\">\nLorem ipsum dolor sit amet, Donec posuere nibh egestas fermentum consequat.</div>\n<div style=\"font-family: 'Open Sans', Arial, sans-serif; font-size: 14px; margin-bottom: 15px; padding: 0px; text-align: justify;\">\n<div class=\"separator\" style=\"clear: both; text-align: center;\">\n<a href=\"https://1.bp.blogspot.com/-HrIjp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."

而且我需要图像的地址,所以我尝试使用以下代码在 playground 中进行模拟:

let string = "<image is at: href=\u{5C}\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."

let stringInit = string 
let stringOperation = NSString(string: stringInit)
let stringArray = stringOperation.components(separatedBy: "href=\u{5C}\u{22}")
let intermediateTwo = stringArray[1]
let operationTwo = NSString(string: intermediateTwo)
let strinArrayTwo = operationTwo.components(separatedBy: ".png")

let image = strinArrayTwo[0] + ".png"

它运行良好,但是当我尝试在我的应用程序中使用它时,它崩溃了,因为 stringArray 只有一个元素。我改变了这一行:

 let stringArray = stringOperation.components(separatedBy: "href=\u{5C}\u{22}")

到:

let stringArray = stringOperation.components(separatedBy: "href=")

它工作正常,我猜它在 PlayGround 中工作,因为我必须以与查找它相同的方式输入数据,但我真的不知道还能尝试什么,所以我的问题是,有没有有什么办法让它正常工作?

谢谢

最佳答案

我不确定你是如何检测到你的content的,如果它是直接从JSON文本中获取的,头部应该是"content":,而不是"内容 =.

但无论如何,它的实际内容似乎是以转义格式表示的。 (JSON 文本使用这种格式来表示 JSON 字符串,以及 Swift String 的调试输出。)

当这种转义格式表示被读入 Swift String 时,一些转义序列如 \" 被读取为单个字符 "

因此,当您将字符串的某些部分替换为 unicode 转义表示时,您需要将 \" 替换为 \u{22},而不是 \你{5c}\u{22}.

此外,您还应该始终检查 components(separatedBy:) 的结果,以避免因意外结果而崩溃。

所以,你的代码应该是这样的:

let string = "<image is at: href=\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."

let stringArray = string.components(separatedBy: "href=\u{22}")
if stringArray.count > 1 {
    let intermediateTwo = stringArray[1]
    let strinArrayTwo = intermediateTwo.components(separatedBy: ".png")

    if strinArrayTwo.count > 1 {
        let image = strinArrayTwo[0] + ".png"
    } else {
        print("'.png' not found")
    }
} else {
    print("'href=' not found")
}

但是你最好考虑使用正则表达式来提取具有这种模式的子字符串。

let pattern = "href=\"(.*?\\.png)\""
let regex = try! NSRegularExpression(pattern: pattern)
if let match = regex.firstMatch(in: string, options: [], range: NSRange(0..<string.utf16.count)) {
    let image = (string as NSString).substring(with: match.rangeAt(1))
    print(image) //->https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png
} else {
    print("'href=...png' not found")
}

关于swift - 将具有特殊字符的字符串分隔成数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645968/

相关文章:

ios - 使用边框初始化构建 UIButton

c++ - String.erase 给出 out_of_range 异常

ios - 如何从 modalVC 中删除 VC 1 中的 tableView 行?

javascript - Jquery仅转义字符串的一部分

JAVA:字符串问题的 128 位 key 和返回

c++ - 在 C++ 中读取 unicode 输入的问题

python - 使用 lxml.html 的 cssselect 选择 ID 属性中带有冒号的元素

python - Python 中图像中文本的 Unicode 问题

swift - 使用 GeometryReader 和 Padding 时的无限循环

swift - 我如何在 XCODE 8 和 Swift 3 中使用 UIAutomation