Swift: A one-line method to add insert functionality to String instances (Xcode 6 Beta 5)
// create an extension for String
extension String {
// create an insert method that returns a string
func insert(string:String,ind:Int) -> String {
// use prefix and suffix algorithms to construct the new string
return prefix(self,ind) + string + suffix(self,countElements(self)-ind)
}
}
// call the new method
"Hello World!".insert("Swift ", ind: 6)
This was first featured on my blog.
Written by Anthony Levings
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ios
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#