Last active
December 7, 2017 18:44
-
-
Save erica/2ea5ccd9fa5fe227a3a30c3663f6a99d to your computer and use it in GitHub Desktop.
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
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