Ben Ben - 1 month ago 11
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
var name: String = "Dolphin"
let endIndex = name.index(name.endIndex, offsetBy: -2)
var truncated = name.substring(to: endIndex)
print(name)      // "Dolphin"
print(truncated) // "Dolph"