Ben Ben - 10 months ago 107
iOS Question

Remove Last Two Characters in a String (Swift 3.0)

Is there a quick way to remove the last two characters in a String in Swift 3.0? I see there is a simple way to remove the last character as clearly noted here. Do you know how to remove the last two characters? Thanks!

Answer Source
var name: String = "Dolphin"
let endIndex = name.index(name.endIndex, offsetBy: -2)
var truncated = name.substring(to: endIndex)
print(name)      // "Dolphin"
print(truncated) // "Dolph"