3

I have to say that for a "modern" language Swift certainly makes a meal of specifying a range to capture a substring. My question: Is there a way to create a <String.Index> using Range(0...4) or indeed a way to cast the result (of type <Int>) so that it can be used with substringWithRange?

let myString = "HappyDays"
var rangeString = Range(start: advance(myString.startIndex, 5), end: advance(myString.endIndex, -1))
myString.substringWithRange(rangeString) // >>> "Day"

.

// Can this be used to generate a range for substringWithRange?
var hardRange = Range(0...4)
fuzzygoat
  • 26,297
  • 46
  • 162
  • 291

1 Answers1

1

Use an extension on string e.g. stackoverflow.com/a/24144365/1032372

Community
  • 1
  • 1
shim
  • 8,356
  • 10
  • 70
  • 102