Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range(0...4) and substringWithRange?

Tags:

ios

swift

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)
like image 289
fuzzygoat Avatar asked Mar 08 '26 09:03

fuzzygoat


1 Answers

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

like image 130
shim Avatar answered Mar 09 '26 22:03

shim