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

后端 未结 9 1357
自闭症患者
自闭症患者 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条回答
  •  情书的邮戳
    2020-12-14 06:11

    Swift 3

    Use the native Swift approach:

    var welcome = "hello"
    
    welcome.insert("!", at: welcome.endIndex) // prints hello!
    welcome.insert("!", at: welcome.startIndex) // prints !hello
    welcome.insert("!", at: welcome.index(before: welcome.endIndex)) // prints hell!o
    welcome.insert("!", at: welcome.index(after: welcome.startIndex)) // prints h!ello
    welcome.insert("!", at: welcome.index(welcome.startIndex, offsetBy: 3)) // prints hel!lo
    

    If you are interested in learning more about Strings and performance, take a look at @Thomas Deniau's answer down below.

提交回复
热议问题