close

 

let rid = "23456"

let index = rid.index(rid.endIndex, offsetBy: -6, limitedBy: rid.startIndex)

let positionSerialNumber = rid.substring(from: index ?? rid.startIndex)

 

(超出索引可變nil,不會crash)

 

(可取出倒數6位字串)

 

參考資料:

https://developer.apple.com/reference/swift/indexable/1779407-index

http://stackoverflow.com/questions/25113672/how-do-you-get-the-last-character-of-a-string-without-using-array-on-swift

arrow
arrow
    全站熱搜

    小賢 發表在 痞客邦 留言(0) 人氣()