编辑/更新:
Xpre 10.2•Swift 5或更高版本
我们可以
StringProtocol使用集合拆分方法进行扩展
func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Character) throws -> Bool) rethrows -> [Self.SubSequence]
设置
omittingEmptySubsequences为true并通过闭包作为谓词。我们还可以利用new
Character属性
isLetter来分割字符串。
extension StringProtocol { var words: [SubSequence] { return split { !$0.isLetter } }}let sentence = "• Hello, Playground!"let words = sentence.words // ["Hello", "Playground"]



