How to add a character at a particular index in string in Swift

后端 未结 9 1340
自闭症患者
自闭症患者 2020-12-14 05:30

I have a string like this in Swift:

var stringts:String = \"3022513240\"

If I want to change it to string to something like this: \"(

9条回答
  •  萌比男神i
    2020-12-14 06:18

    You can't use in below Swift 2.0 because String stopped being a collection in Swift 2.0. but in Swift 3 / 4 is no longer necessary now that String is a Collection again. Use native approach of String,Collection.

    var stringts:String = "3022513240"
    let indexItem = stringts.index(stringts.endIndex, offsetBy: 0)
    stringts.insert("0", at: indexItem)
    print(stringts) // 30225132400
    

提交回复
热议问题