0

If I have a string such as xAM - yPM is it possible to split the string into 2 strings using the - as the point where the string is split?

Stewart Hering
  • 304
  • 4
  • 14
  • 1
    Check [this](http://stackoverflow.com/questions/25678373/swift-split-a-string-into-an-array) and [this](http://stackoverflow.com/questions/25818197/how-to-split-a-string-in-swift) – Bista Mar 25 '15 at 11:44

1 Answers1

1

You can use the componentsSeparatedByString function:

var splittedArray = yourString.componentsSeparatedByString(" - ")
println(splittedArray[0]) // "xPM"
println(splittedArray[1]) // "yPM"
Christian
  • 22,585
  • 9
  • 80
  • 106
  • The answer is incorrect, there is a compile error,. Only a character is allowed as a split character, not a String. There is also a typo: there must be a space prior to the `=` in the first line. Array index `0` is used twice. The result and trading and leading space characters respectively. – zaph Mar 25 '15 at 12:24
  • Why a not use componentsSeparatedByString, it has two added benefits: 1. The separator is not limited to a single character. 2. There is no extra '$0' in a closure. – zaph Mar 25 '15 at 12:24
  • @Zaph Thanks for the correction. Don't know why I didn't use it in first place. – Christian Mar 25 '15 at 12:26
  • I've got no access to a IDE at the moment. But that's no excuse. Fixed it. – Christian Mar 25 '15 at 12:30