Simply convert that last character
to String
and then String
to Int
.
if let last = str.characters.last, let value = Int(String(last)) {
print(value)
}
Edit: If you are having a number like cart10,cart11,…cart100… then to get the number after cart try this way.
let str = "cart15"
let cartNumber = str.characters.flatMap({Int(String($0))}).reduce(0, {10 * $0 + $1})
print(cartNumber) //15
1
solved How To get and convert last character of string into Int in Swift 3?