Last edit
Changed:
< var str = "Hello, playground"
to
> let str = "Hello, playground"
Changed:
< if len > stepsize {
< for start in stride(from: 0, to: len, by: stepsize) {
< var end = start + stepsize
< if end > len {
< end = len
< }
<
< let startIndex = str.index(str.startIndex, offsetBy: start)
< let endIndex = str.index(str.startIndex, offsetBy: end)
< print(str[startIndex..<endIndex])
to
> for start in stride(from: 0, to: len, by: stepsize) {
> var end = start + stepsize
> if end > len {
> end = len
Added:
>
> let startIndex = str.index(str.startIndex, offsetBy: start)
> let endIndex = str.index(str.startIndex, offsetBy: end)
> print("From \(start) to \(end)")
> print(str[startIndex..<endIndex])
This is how you walk through a string in Swift 3:
let str = "Hello, playground" let stepsize = 3 let len = str.characters.count
for start in stride(from: 0, to: len, by: stepsize) { var end = start + stepsize if end > len { end = len } let startIndex = str.index(str.startIndex, offsetBy: start) let endIndex = str.index(str.startIndex, offsetBy: end)
print("From \(start) to \(end)") print(str[startIndex..<endIndex]) }
Note: not optimized in any way.