Swift String Extension to Trim Trailing Punctuation
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 trimTrailingPunctuation() -> String { | |
return self.trimmingCharacters(in: .whitespacesAndNewlines) | |
.trimmingCharacters(in: .punctuationCharacters) | |
.trimmingCharacters(in: .whitespacesAndNewlines) | |
} | |
} |
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
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