ios - 从字符串中获取单词列表

标签 ios arrays string swift3

我是 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/

相关文章:

python - 任何存在内存使用的对象,如生成器,但可以在必要时返回一个 numpy 数组?

JavaScript 字符串搜索

c++ - 将对象格式化为字符串

objective-c - 将经纬度转换为地名

ios - 当需要后台模式访问时,如何在 iOS 上安全地存储数据?

ios - 在 Swift 中呈现 View Controller 时关于不可见标签栏的问题

ios - Xcode 9 - iOS 11 设备日志私有(private)

javascript - 如果测试[未定义]

jquery - 如何将整数数组从 asp.net mvc Controller 操作传递回 jquery 函数

javascript - jquery从字符串到时间的转换