Splitting a String into an Array

suggest change

components(separatedBy:) splits a string on a separator. It is a Foundation method. Swift 2 called it componentsSeparatedByString.

let startDate = "23:51"

let startDateAsArray = startDate.components(separatedBy: ":") // ["23", "51"]

Swift 2:

let startDate = "23:51"

let startArray = startDate.componentsSeparatedByString(":") // ["23", "51"]

Or when the separator isn’t present:

let myText = "MyText"

let myTextArray = myText.components(separatedBy: " ") // myTextArray is ["MyText"]

Swift 2:

let myText = "MyText"

let myTextArray = myText.componentsSeparatedByString(" ") // myTextArray is ["MyText"]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents