Remove Last Two Characters in a String

前端 未结 5 812
旧巷少年郎
旧巷少年郎 2020-12-13 03:15

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

5条回答
  •  春和景丽
    2020-12-13 04:11

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

提交回复
热议问题