Last active
July 23, 2016 10:01
-
-
Save artemkrachulov/98f4288efc50fc5c42eb8c812cd6b113 to your computer and use it in GitHub Desktop.
Convert Range<Int> to Range<String.Index> object
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
// | |
// Range+RangeIntToRangeStringIndex.swift | |
// | |
// Created by Artem Krachulov. | |
// Copyright (c) 2016 Artem Krachulov. All rights reserved. | |
// Website: http://www.artemkrachulov.com/ | |
// | |
import UIKit | |
/// Convert Range<Int> to Range<String.Index> object. | |
/// | |
/// Usage: | |
/// | |
/// let str = "Hello World!" | |
/// let toRangeStringIndex = converRangeIntToRangeStringIndex(str, range: 6..<11) // 6..<11 | |
public func RangeIntToRangeStringIndex(str: String, range: Range<Int>) -> Range<String.Index>? { | |
guard range.startIndex <= str.characters.count && range.endIndex <= str.characters.count else { | |
return nil | |
} | |
return Range<String.Index>(str.startIndex.advancedBy(range.startIndex)..<str.startIndex.advancedBy(range.endIndex)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment