Remove nth character from string

前端 未结 4 1512
无人及你
无人及你 2021-01-05 01:30

I have seen many methods for removing the last character from a string. Is there however a way to remove any old character based on its index?

4条回答
  •  伪装坚强ぢ
    2021-01-05 01:54

    Here is a safe Swift 4 implementation.

    var s = "Hello, I must be going"
    var n = 5
    if let index = s.index(s.startIndex, offsetBy: n, limitedBy: s.endIndex) {
        s.remove(at: index)
        print(s) // prints "Hello I must be going"
    } else {
        print("\(n) is out of range")
    }
    

提交回复
热议问题