Created
February 10, 2017 20:36
-
-
Save maximbilan/47dc888ae8e2b8bfef1d4824fb6de441 to your computer and use it in GitHub Desktop.
Swift 3 Trimming of special characters
This file contains 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
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