Skip to content

Instantly share code, notes, and snippets.

@kanetai
Last active August 3, 2017 15:34
Show Gist options
  • Save kanetai/de59f923a2e7cfcec377f90bfd974861 to your computer and use it in GitHub Desktop.
Save kanetai/de59f923a2e7cfcec377f90bfd974861 to your computer and use it in GitHub Desktop.
[Swift]String extension for competitive programming
//Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81)
extension String: CollectionType {
func at(idx: Int) -> Character {
return self[self.startIndex.advancedBy(idx)]
}
func subString(s: Int, _ e: Int) -> String {
return self[self.startIndex.advancedBy(s)..<self.startIndex.advancedBy(e)]
}
}
"abcdef".at(1) //"b": Character
"012345".count //6
"012345".subString(2, 5) //"234"
"abc3defg7".split { "0"..."9" ~= $0 } //["abc", "defg"]
"4 5a 5 a 7 ".split(" ").flatMap { Int($0) } //[4, 5, 7]
["a", "b", "c"].joinWithSeparator("+") //"a+b+c"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment