我是 swift 的新手。我需要从前缀为 #
的字符串中获取单词列表。这是我的字符串
“在 6 月 14 日的火灾后,确认有 30 人死亡,但预计死亡总数会更高。......一名男子在#Facebook 上张贴了据信死者尸体的照片从#Grenfell Tower 跳楼身亡#fire 已被#jail 三个月"
我需要流动的单词列表:
["#Facebook","#Grenfell","#fire","#jailed"]
我花了很多时间但无法弄清楚如何实现这一目标。
最佳答案
您可以这样做:
let str = "Thirty people are confirmed dead after the June 14 fire, but the total is expected to rise far higher. ... A man who posted pictures on #Facebook of the body of someone believed to have leapt to his death from the #Grenfell Tower #fire has been #jailed for three months"
let words = str.components(separatedBy: " ").filter { (element) -> Bool in
return element.hasPrefix("#")
}
print(words)
关于ios - 从字符串中获取单词列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643318/