Last active
October 18, 2017 04:45
-
-
Save mminer/a6757e2a55b5fa4524efa2a0f89a7033 to your computer and use it in GitHub Desktop.
String extension that adds a method to insert a string in the middle of it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func insert(_ string: String, at index: Int) -> String { | |
let prefix = String(characters.prefix(index)) | |
let suffix = String(characters.suffix(characters.count - index)) | |
return prefix + string + suffix | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is no longer useful now that Swift 4 improved string handling, in particular
insert(contentsOf:at:)
.