Last active
May 18, 2019 00:14
-
-
Save xieweiAlex/74090403526662f18a468ca0f9d630db to your computer and use it in GitHub Desktop.
Swift String extension for subString
This file contains hidden or 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
``` Swift | |
extension String { | |
func index(from: Int) -> Index { | |
return self.index(startIndex, offsetBy: from) | |
} | |
func substring(from: Int) -> String { | |
let fromIndex = index(from: from) | |
return substring(from: fromIndex) | |
} | |
func substring(to: Int) -> String { | |
let toIndex = index(from: to) | |
return substring(to: toIndex) | |
} | |
func substring(with r: Range<Int>) -> String { | |
let startIndex = index(from: r.lowerBound) | |
let endIndex = index(from: r.upperBound) | |
return substring(with: startIndex..<endIndex) | |
} | |
} | |
``` |
Credit to StackOverflow answer: https://stackoverflow.com/questions/39677330/how-does-string-substring-work-in-swift
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ex: