Henny Lee Henny Lee - 1 year ago 158
Swift Question

Splitting String using CharacterSet

In Swift 2.x I was able to do:

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)

But in Swift 3.x it has changed. Can someone tell me how to use this in Swift 3?

Answer Source

Answer given by @jjatie is correct but it will also include empty String elements in an Array, you can simply remove empty String elements using filter.

let str = "Line 1\nLine 2\r\nLine 3\n"
let lines = str.components(separatedBy: .newlines).filter { !$0.isEmpty }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download