Last active
August 30, 2019 14:54
-
-
Save DanielCardonaRojas/d1173906b80dd33d279cebbc58373ed5 to your computer and use it in GitHub Desktop.
StringExtensions
This file contains hidden or 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 isContainedBy(set: CharacterSet) -> Bool { | |
let letters = self.components(separatedBy: set) | |
print(letters) | |
return self.count == letters.count | |
} | |
func filtered(by set: CharacterSet) -> String { | |
let chars = self.unicodeScalars.filter({ set.contains($0)}) | |
let unicode = UnicodeScalarView(chars) | |
return String(unicode) | |
} | |
func trimEnd(while predicate: (Character) -> Bool) -> String { | |
var newString = self | |
while let lastChar = newString.last, predicate(lastChar) { | |
newString = String(newString.dropLast()) | |
} | |
return newString | |
} | |
func trimmingEndCharacters(in set: CharacterSet) -> String { | |
let predicate: (Character) -> Bool = { char in !char.unicodeScalars.filter({ set.contains($0) }).isEmpty } | |
return trimEnd(while: predicate) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment