Ben Ben - 1 year ago 203
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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download