Skip to content

Instantly share code, notes, and snippets.

@algal
Created November 28, 2016 01:49
Show Gist options
  • Save algal/c21f0dd47c14886ff50d13e6a81f2349 to your computer and use it in GitHub Desktop.
Save algal/c21f0dd47c14886ff50d13e6a81f2349 to your computer and use it in GitHub Desktop.
Mutating in-place replacement of a substring of a String
// known-good Swift3
extension String {
/// Performs in-place replacement of first occurence of a substring
mutating func replaceFirstOccurence(of query:String, with replacement:String) {
guard let firstRange = self.range(of: query)
else { return }
self.replaceSubrange(firstRange, with: replacement)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment