Clément Bisaillon Clément Bisaillon - 1 year ago 91
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 Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download