Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active October 18, 2017 04:45
Show Gist options
  • Save mminer/a6757e2a55b5fa4524efa2a0f89a7033 to your computer and use it in GitHub Desktop.
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.
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
}
}
@mminer
Copy link
Author

mminer commented Oct 18, 2017

This is no longer useful now that Swift 4 improved string handling, in particular insert(contentsOf:at:).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment