Skip to content

Instantly share code, notes, and snippets.

@tjhv
Created June 10, 2019 13:30
Show Gist options
  • Save tjhv/27e2b86b1ca475523401f28ab6178455 to your computer and use it in GitHub Desktop.
Save tjhv/27e2b86b1ca475523401f28ab6178455 to your computer and use it in GitHub Desktop.
extension String
{
func character(at index: Int) -> Character?
{
guard index <= self.count && index >= -self.count && index != 0 else {
return nil
}
return Array(self)[(index > 0 ? index - 1 : self.count + index)]
}
func occurrences(of character: Character) -> Int
{
return (self.filter { $0 == character } as String).count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment