美文网首页
从有很多标点的 String 中取出所有单词

从有很多标点的 String 中取出所有单词

作者: 冰霜海胆 | 来源:发表于2018-04-17 15:49 被阅读9次
var str = "Hello, playground. Have a nice day!"

extension String {
    
    var words: [String] {
        return components(separatedBy: .punctuationCharacters)
        .joined()
        .components(separatedBy: .whitespaces)
        .filter({ !$0.isEmpty })
    }
    
}

print(str.words)

相关文章

网友评论

      本文标题:从有很多标点的 String 中取出所有单词

      本文链接:https://www.haomeiwen.com/subject/iwqpkftx.html