Swift Language Splitting a String into an Array


Example

In Swift you can easily separate a String into an array by slicing it at a certain character:

3.0
let startDate = "23:51"

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

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

Or when the separator isn't present:

3.0
let myText = "MyText"

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

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