Skip to content

Instantly share code, notes, and snippets.

@masters3d
Forked from Brimizer/swift_range_convert
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masters3d/9cfdb6583ad0459a20f9 to your computer and use it in GitHub Desktop.
Save masters3d/9cfdb6583ad0459a20f9 to your computer and use it in GitHub Desktop.
// Public domain.
// Free to use.
// Use like this:
var world = "Hello, world!"
let convertedRange = world.convertRange(0..<5)
world.removeRange(convertedRange)
// Converts a regular range (0..5) to a proper String.Index range.
extension String {
public func convertRange(range: Range<Int>) -> Range<String.Index> {
let startIndex = advance(self.startIndex, range.startIndex)
let endIndex = advance(startIndex, range.endIndex - range.startIndex)
return Range<String.Index>(start: startIndex, end: endIndex)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment