Skip to content

Instantly share code, notes, and snippets.

@erica
Last active December 7, 2017 18:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/2ea5ccd9fa5fe227a3a30c3663f6a99d to your computer and use it in GitHub Desktop.
Save erica/2ea5ccd9fa5fe227a3a30c3663f6a99d to your computer and use it in GitHub Desktop.
import Foundation
public extension String {
public func replacing(range: CountableClosedRange<Int>, with replacementString: String) -> String {
let start = characters.index(characters.startIndex, offsetBy: range.lowerBound)
let end = characters.index(start, offsetBy: range.count)
return self.replacingCharacters(in: start ..< end, with: replacementString)
}
}
var test = "Forty Two"
for index in 1...test.characters.count {
print(test.replacing(range: 1 ... index, with: "XXXX"))
}
// See also:
infix operator ..+ {}
public func ..+ <Bound: Strideable>(lhs: Bound, rhs: Bound.Stride) -> CountableClosedRange<Bound> {
return lhs ... lhs.advanced(by: rhs)
}
test.replacing(range: 1 ..+ 2, with: "XXXX")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment