Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active January 10, 2016 12:01
Show Gist options
  • Save oisdk/284e06f6c23b0d964cf7 to your computer and use it in GitHub Desktop.
Save oisdk/284e06f6c23b0d964cf7 to your computer and use it in GitHub Desktop.
let digs: Set<Character> = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
extension String {
var firstNum: Int? {
return self
.characters
.indexOf(digs.contains)
.map(characters.suffixFrom)
.flatMap { cs in
Int(String(cs.prefixUpTo(cs.indexOf { c in !digs.contains(c) } ?? cs.endIndex)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment