Created
October 25, 2017 05:06
-
-
Save mike-neko/654a24a08223fcd6dad88c9e28762a82 to your computer and use it in GitHub Desktop.
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 rightTrimmingCharacters(in set: CharacterSet) -> String { | |
for c in characters.reversed().enumerated() { | |
let isMatch = c.element.unicodeScalars.contains { set.contains($0) } | |
if !isMatch { | |
return String(self[startIndex..<index(endIndex, offsetBy: -c.offset)]) | |
// swift4 | |
//return String(prefix(upTo: index(endIndex, offsetBy: -c.offset))) | |
} | |
} | |
return "" | |
} | |
func leftTrimmingCharacters(in set: CharacterSet) -> String { | |
for c in characters.enumerated() { | |
let isMatch = c.element.unicodeScalars.contains { set.contains($0) } | |
if !isMatch { | |
return String(self[index(startIndex, offsetBy: c.offset)..<endIndex]) | |
// swift4 | |
//return String(suffix(from: index(startIndex, offsetBy: c.offset))) | |
} | |
} | |
return "" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment