Clément Bisaillon Clément Bisaillon - 5 months ago 17
Swift Question

How to split a string in Swift

let string = "hello hi"
var hello = ""
var hi = ""


I wan't to split my string so that the value of hello get "hello" and the value of hi get "hi"

Answer

Try this:

var myString: String = "hello hi";
var myStringArr = myString.componentsSeparatedByString(" ")

Where myString is the name of your string, and myStringArr contains the components separated by the space.

Then you can get the components as:

var hello: String = myStringArr [0]
var hi: String = myStringArr [1]