使用字符集拆分字符串

Hen*_*Lee 3 string swift swift3

在 Swift 2.x 中,我能够做到:

let str = "Line 1\nLine 2\r\nLine 3\n"
let newlineChars = NSCharacterSet.newlineCharacterSet()
let lines = str.utf16.split { newlineChars.characterIsMember($0) }.flatMap(String.init)
Run Code Online (Sandbox Code Playgroud)

但是在 Swift 3.x 中它发生了变化。有人能告诉我如何在 Swift 3 中使用它吗?

jja*_*tie 5

现在这在 Swift 3 中更简单一些。

let str = "Line 1\nLine 2\r\nLine 3\n"
let newlineChars = NSCharacterSet.newlines
let lines = str.components(separatedBy: newlineChars)
    .filter{ !$0.isEmpty }
Run Code Online (Sandbox Code Playgroud)

或者干脆

let str = "Line 1\nLine 2\r\nLine 3\n"
let lines = str.components(separatedBy: .newlines)
    .filter{ !$0.isEmpty }
Run Code Online (Sandbox Code Playgroud)