Last active
February 3, 2019 20:01
-
-
Save benbahrenburg/7c6cb8330732b82d4c0e72854ecd817b to your computer and use it in GitHub Desktop.
Swift String Extension to Trim Trailing Punctuation
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 trimTrailingPunctuation() -> String { | |
return self.trimmingCharacters(in: .whitespacesAndNewlines) | |
.trimmingCharacters(in: .punctuationCharacters) | |
.trimmingCharacters(in: .whitespacesAndNewlines) | |
} | |
} |
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
let example1 = "How are you???".trimTrailingPunctuation() | |
>>> How are you | |
let example2 = "Hi!!!!".trimTrailingPunctuation() | |
>>> Hi | |
let example3 = "Act limited time offer now!".trimTrailingPunctuation() | |
>>> Act limited time offer now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment