Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Last active February 3, 2019 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbahrenburg/7c6cb8330732b82d4c0e72854ecd817b to your computer and use it in GitHub Desktop.
Save benbahrenburg/7c6cb8330732b82d4c0e72854ecd817b to your computer and use it in GitHub Desktop.
Swift String Extension to Trim Trailing Punctuation
extension String {
func trimTrailingPunctuation() -> String {
return self.trimmingCharacters(in: .whitespacesAndNewlines)
.trimmingCharacters(in: .punctuationCharacters)
.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
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