Skip to content

Instantly share code, notes, and snippets.

@maximbilan
Created February 10, 2017 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximbilan/47dc888ae8e2b8bfef1d4824fb6de441 to your computer and use it in GitHub Desktop.
Save maximbilan/47dc888ae8e2b8bfef1d4824fb6de441 to your computer and use it in GitHub Desktop.
Swift 3 Trimming of special characters
extension String {
func trim(_ string: String) -> String {
var set = Set<Character>()
for c in string.characters {
set.insert(Character(String(c)))
}
return trim(set)
}
func trim(_ characters: Set<Character>) -> String {
if let index = self.characters.index(where: {!characters.contains($0)}) {
return self[index..<self.endIndex]
} else {
return ""
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment