Remove nth character from string

前端 未结 4 1516
无人及你
无人及你 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:46

    Swift 3.2

    let str = "hello"
    let position = 2
    let subStr = str.prefix(upTo: str.index(str.startIndex, offsetBy: position)) + str.suffix(from: str.index(str.startIndex, offsetBy: (position + 1)))
    print(subStr)
    

    "helo"

提交回复
热议问题