Pure Swift: A one-line method to add insert functionality to String instances (Xcode 6.3; update for Xcode 7, iOS 9)


This is the third blogpost focusing on writing pure swift methods and extensions. And here's a really simple one to add insert functionality to String instances:
extension String {
    func insert(string:String,ind:Int) -> String {
        return  prefix(self,ind) + string + suffix(self,count(self)-ind)
    }
}

"Hello World!".insert("Swift ", ind: 6)

Enjoy!

Update: 14 April 2015

I wrote this post in the early days of Swift and in fact there's a much easier way of inserting a string called splice().
var aString = "Hello World!"
aString.splice("Swift ", atIndex: advance(str.startIndex,6))
So while there are any number of ways that we can approach the insertion of a String without splice, for example using subscripting (inspired by a StackOverflow post):
extension String {
    mutating func insert(str:String, atIndex:String.Index) {
        self = self[Range(start: self.startIndex,end: atIndex)] + str + self[Range(start: atIndex,end: self.endIndex)]
    }
}

var hi = "Hello World!"
hi.insert("Swift ", ind: advance(hi.startIndex,6))
Or using loops, perhaps reversing the string first if we felt so inclined so that the insert keeps happening at the same index:
extension String {
    mutating func insert(str:String, atIndex:String.Index) {
        for c in reverse(str) {
            self.insert(c, atIndex: atIndex)
        }
    }
}

var hi = "Hello World!"
hi.insert("Swift ", ind: advance(hi.startIndex,6))
Or going for an even more traditional looping approach:
extension String {
    mutating func insert(string:String, atIndex:String.Index) {
        var index = string.startIndex
        for c in string {
            self.insert(c, atIndex: index)
            index = advance(index, 1)
        }
    }
}
var hi = "Hello World!"
hi.insert("Swift ", ind: advance(hi.startIndex,6))
All we really need is to become accustomed to using splice().

Update: 3 November 2015 (Xcode 7, iOS 9)

As identified by EL Houcine Az in the comments below, splice() is no longer available in Xcode 7, iOS 9. The code becomes instead:
aString.insertContentsOf("Swift ".characters, at: aString.startIndex.advancedBy(6))  // "Hello Swift World!"
And we can write the following string extension to simply our code:
extension String {
    mutating func insert(string:String,ind:Int) {
        self.insertContentsOf(string.characters, at: string.startIndex.advancedBy(ind))
    }
}

var aStr = "Hello World!"
aStr.insert("Swift ", ind: 6) // "Hello Swift World!"
which means it's goodbye splice(), hello insertContentsOf() as Apple moves away from the sleek and functional naming conventions of early Swift back to more descriptive ones à la Objective-C.

Endorse on Coderwall

Comments

  1. I think you should update this post since many things have changed in swift 2, and splice() has been removed.

    ReplyDelete
    Replies
    1. Thanks for taking the time to comment and prompting me to update code. Please see above use of insertContentsOf()

      Delete

Post a Comment